使用ColdFusion将文件上传到Google云端硬盘 [英] Upload Files To Google Drive Using ColdFusion

查看:134
本文介绍了使用ColdFusion将文件上传到Google云端硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*新增更好的第二部分 - 现在获得到308恢复不完整,即使文件应该只是一个上传!



我使用Ray Camden的 cfgoogle 基础。但Google已弃用文档上传的代码。新标准为可恢复的媒体上传



我在上述引用的Google文档中,此部分功能正常(包括启动可恢复上传请求)。



调用页面:

 < 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)>

代码工作到并包括 cfset temp line(获取唯一的上传URI)



以下是upload_auth的代码:

 < cffunction name =upload_authaccess =publicreturnType =anyhint =我从Google API获取唯一URI。 output =false> 
< cfargument name =myFiletype =stringrequired =truehint =要上传的文件路径。>
< cfargument name =myTypetype =stringrequired =truehint =application / msword>
< cfargument name =myTitletype =stringrequired =truehint =文件名>

< cfset GoogleUrl =https://docs.google.com/feeds/upload/create-session/default/private/full\">
< cfset GoogleVersion = 3>
< cfset FileSize = createObject(java,java.io.File)。init(myFile).length()>

< cfhttp url =#GoogleUrl#method =postresult =diditworkresolveurl =no>
< cfhttpparam type =headername =Authorizationvalue =GoogleLogin auth =#getAuth(variables.docservice)#>
< cfhttpparam type =headername =GData-Versionvalue =#GoogleVersion#>
< cfhttpparam type =headername =Content-Lengthvalue =0>
< cfhttpparam type =headername =X-Upload-Content-Typevalue =#myType#>
< cfhttpparam type =headername =X-Upload-Content-Lengthvalue =#FileSize#>
< cfhttpparam type =headername =Slugvalue =#myTitle#>

< / cfhttp>

< cfreturn diditwork>
< / cffunction>

好 - 太好了。
但是这里是它分解的地方:



运行upload_file从Google返回308 Resume Incomplete(一个不是400! Arrgh!



以下是upload_file -

 < cffunction name = upload_fileaccess =publicreturnType =anyhint =我上传文档。 output =false> 
< cfargument name =myFiletype =stringrequired =truehint =要上传的文件路径。>
< cfargument name =myTypetype =stringrequired =truehint =like application / msword>
< cfargument name =myTitletype =stringrequired =truehint =文件名>
< cfargument name =myAuthPathtype =stringrequired =truehint =call auth>

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

< CFFILE action =readfile =#myfile#variable =FileText>

< cfhttp url =#myAuthPath#method =putresult =diditworkresolveurl =nomultipart =yescharset =utf-8>
< cfhttpparam type =headername =Authorizationvalue =GoogleLogin auth =#getAuth(variables.docservice)#>
< cfhttpparam type=headername =GData-Versionvalue =#variables.GoogleVersion#>
< cfhttpparam type =headername =Content-Lengthvalue =#FileSize#>
< cfhttpparam type =headername =Content-Rangevalue =bytes 0-#tv.tostartwithzero#/#FileSize#>
< cfhttpparam type =headername =Content-Typevalue =#myType#>

< cfhttpparam type =bodyvalue =#trim(FileText)#>

< / cfhttp>

< cfreturn diditwork>
< / cffunction>

所以,我们有它 -
我可以得到唯一的URI,但(可能是因为它是深夜)我的脑死了我做错了否则完成文件上传。


$ b $

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




  • 看来代码使用的是已弃用的客户端登录身份验证机制,



  • 使用已弃用的文档列表API而不是较新的Drive API。
  • $ b

    因为你可以调用java,我建议编写上传代码在纯Java,然后根据需要从你的CF页面调用它。


    *NEW UPDATED FOR BETTER SECOND PART - NOW GETS TO "308 Resume Incomplete", even though file should be just one upload!

    I am using the foundation of cfgoogle from Ray Camden. But Google has deprecated the code for document uploads. The new standard is Resumable Media Uploads.

    I have this part working (up to and including the "Initiating a resumable upload request") in the above referenced Google document.

    Calling Page:

    <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)>
    

    The code works up to and including the cfset temp line (getting the unique upload URI)

    Here is the code for upload_auth:

    <cffunction name="upload_auth" access="public" returnType="any" hint="I get a uniqu URI from Google API." output="false">
    <cfargument name="myFile" type="string" required="true" hint="filepath to upload.">
    <cfargument name="myType" type="string" required="true" hint="application/msword"> 
    <cfargument name="myTitle" type="string" required="true" hint="name of doc"> 
    
    <cfset GoogleUrl = "https://docs.google.com/feeds/upload/create-session/default/private/full">
    <cfset GoogleVersion = 3> 
    <cfset FileSize = createObject("java","java.io.File").init(myFile).length()>
    
    <cfhttp url="#GoogleUrl#" method="post" result="diditwork" resolveurl="no">
    <cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#getAuth(variables.docservice)#">
    <cfhttpparam type="header" name="GData-Version" value="#GoogleVersion#">
    <cfhttpparam type="header" name="Content-Length" value="0">
    <cfhttpparam type="header" name="X-Upload-Content-Type" value="#myType#">
    <cfhttpparam type="header" name="X-Upload-Content-Length" value="#FileSize#">
    <cfhttpparam type="header" name="Slug" value="#myTitle#">
    
    </cfhttp>
    
    <cfreturn diditwork>
    </cffunction>
    

    OK - So Far So Good. But here is where it breaks down:

    Running upload_file returns "308 Resume Incomplete" (A lest it's not a 400!) from Google. Arrgh!!

    Here is the upload_file -

    <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>
    

    So, there we have it - where I am stuck. I can get the unique URI, but (maybe because it is late at night) I'm brain dead on what I am doing wrong otherwise to complete the file upload.

    All help is appreciated.

    解决方案

    I strongly recommend taking a different approach here. There are two big issues with the path you're taking:

    • It appears the code is using the deprecated client login auth mechanism and is using the username/password instead of OAuth.
    • Using the deprecated documents list API instead of the newer Drive API.

    Since you can call java, I'd suggest writing the upload code in plain Java then invoking it from your CF pages as needed.

    这篇关于使用ColdFusion将文件上传到Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆