使用ColdFusion将文件上载到Google Drive

使用ColdFusion将文件上载到Google Drive,coldfusion,google-drive-api,cfml,Coldfusion,Google Drive Api,Cfml,*新更新的更好的第二部分-现在得到“308简历不完整”,即使文件应该只是一个上传 < >我使用Ray Camden的代码< CfGoGoo> 。但谷歌不赞成上传文档的代码。新标准是 我在上面提到的谷歌文档中有这部分内容(包括“启动可恢复上传请求”) 呼叫页面: <cfset application.cfc.Google = createObject('component','#path_cf_cfc#Google') /> <cfset ap

*新更新的更好的第二部分-现在得到“308简历不完整”,即使文件应该只是一个上传

< >我使用Ray Camden的代码< CfGoGoo> <代码>。但谷歌不赞成上传文档的代码。新标准是

我在上面提到的谷歌文档中有这部分内容(包括“启动可恢复上传请求”)

呼叫页面:

<cfset application.cfc.Google                   = createObject('component','#path_cf_cfc#Google') />
<cfset application.cfc.GoogleDocs               = createObject('component','#path_cf_cfc#GoogleDocs') />

<cfset gtoken = application.cfc.GoogleDocs.authenticate(emailaddress,password)>

<CFSET testdoc = "a\filepath\documentname.doc">
<CFSET FileType = "application/msword">
<CFSET FileTitle = "test_001">

<cfset temp = application.cfc.GoogleDocs.upload_auth("#Application.Map.DocStorage##tv.testdoc#",FileType,FileTitle)>  

<CFSET uploadpath = Listgetat(Listgetat(temp.header,ListContains(temp.header,"https://docs.google.com","#chr(10)#"),"#chr(10)#"),2," ") >  

<cfset temp2 = application.cfc.GoogleDocs.upload_file("#Application.Map.DocStorage##tv.testdoc#",FileType,FileTitle,uploadpath)>
好的,到目前为止还不错。 但这里是它的崩溃之处:

运行upload_文件会从谷歌返回“308简历不完整”(恐怕不是400!)。啊

这是上传文件-

<cffunction name="upload_file" access="public" returnType="any" hint="I upload the document." output="false">
<cfargument name="myFile" type="string" required="true" hint="filepath to upload.">
<cfargument name="myType" type="string" required="true" hint="like application/msword"> 
<cfargument name="myTitle" type="string" required="true" hint="name of doc"> 
<cfargument name="myAuthPath" type="string" required="true" hint="call auth"> 

<cfset FileSize = GetFileInfo(myFile).size >
<CFSET tv.tostartwithzero = FileSize - 1>

<CFFILE action="read" file="#myfile#" variable="FileText">

<cfhttp url="#myAuthPath#" method="put" result="diditwork" resolveurl="no" multipart="yes" charset="utf-8" >
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#getAuth(variables.docservice)#">
<cfhttpparam type="header" name="GData-Version" value="#variables.GoogleVersion#">
<cfhttpparam type="header" name="Content-Length" value="#FileSize#">
<cfhttpparam type="header" name="Content-Range" value="bytes 0-#tv.tostartwithzero#/#FileSize#">
<cfhttpparam type="header" name="Content-Type" value="#myType#">

<cfhttpparam type="body" value="#trim(FileText)#">

</cfhttp>

<cfreturn diditwork>
</cffunction>

所以,我们有它-在那里我被卡住了。 我可以得到唯一的URI,但是(可能是因为现在已经很晚了)我对自己在完成文件上传时所做的错事一无所知


非常感谢您的帮助。

我强烈建议您在这里采取不同的方法。你所走的道路有两大问题:

  • 代码似乎使用了不推荐使用的客户端登录身份验证机制,并且使用的是用户名/密码而不是OAuth
  • 使用不推荐使用的文档列表API而不是较新的驱动器API

由于您可以调用java,我建议您使用纯java编写上载代码,然后根据需要从CF页面调用它。

我注意到,在content range标头中,您使用文件大小作为读取磁盘,但在正文中,您发送的是trim(FileText),这意味着如果文件包含前导或尾随空格,请求正文大小将小于您声称发送的大小。您可以尝试使用len(trim(FileText))来计算在内容范围和内容长度标题中发送的范围和大小?您也可以尝试执行FileReadBinary并在请求正文中发送,除非有理由修剪文件内容。您是否解决了此问题?找到以下内容。在接收到恢复请求的内容体后,服务器可能仍然不具有完整的字节范围,需要客户端采取进一步的操作(例如,额外的请求)。在这种情况下,如果服务器仍愿意继续操作,则应返回状态代码308(恢复未完成)。根据定义,308(Resume Complete)响应表示客户端可以通过发送服务器丢失的字节来纠正当前错误状况
<cffunction name="upload_file" access="public" returnType="any" hint="I upload the document." output="false">
<cfargument name="myFile" type="string" required="true" hint="filepath to upload.">
<cfargument name="myType" type="string" required="true" hint="like application/msword"> 
<cfargument name="myTitle" type="string" required="true" hint="name of doc"> 
<cfargument name="myAuthPath" type="string" required="true" hint="call auth"> 

<cfset FileSize = GetFileInfo(myFile).size >
<CFSET tv.tostartwithzero = FileSize - 1>

<CFFILE action="read" file="#myfile#" variable="FileText">

<cfhttp url="#myAuthPath#" method="put" result="diditwork" resolveurl="no" multipart="yes" charset="utf-8" >
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#getAuth(variables.docservice)#">
<cfhttpparam type="header" name="GData-Version" value="#variables.GoogleVersion#">
<cfhttpparam type="header" name="Content-Length" value="#FileSize#">
<cfhttpparam type="header" name="Content-Range" value="bytes 0-#tv.tostartwithzero#/#FileSize#">
<cfhttpparam type="header" name="Content-Type" value="#myType#">

<cfhttpparam type="body" value="#trim(FileText)#">

</cfhttp>

<cfreturn diditwork>
</cffunction>