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

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

问题描述

*为更好的第二部分进行了新更新 - 现在进入308 Resume Incomplete",即使文件应该只是一个上传!

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

我正在使用 Ray Camden 的 cfgoogle 基础.但是谷歌已经弃用了文件上传的代码.新标准是可恢复媒体上传.

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.

我在上面引用的 Google 文档中让这部分工作(直到并包括发起可恢复的上传请求").

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

调用页面:

<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 = "afilepathdocumentname.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 临时行(获取唯一的上传 URI)之前有效并包括在内

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

这里是upload_auth的代码:

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:

运行 upload_file 从 Google 返回308 Resume Incomplete"(以免不是 400!).啊!!

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

这里是upload_file -

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>

所以,我们有它 - 我被卡住了.我可以获得唯一的 URI,但是(可能是因为它是深夜)我对我做错的事情已经脑死了,否则要完成文件上传.

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.

感谢所有帮助.

推荐答案

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

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

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

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

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 Drive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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