使用JavaScript的Google云端硬盘:处理文件内容 [英] Google Drive using JavaScript: Handling file content

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

问题描述

两个月前,在使用JavaScript的Google云端硬盘授权
@Nivco 撰写关于处理文件内容的文章,这非常诱人:


我们也可以改进文件内容服务器,以便它支持XHR
请求。




<所以,我们在哪里可以找到有关此功能的新闻或路线图,信息等等?







实际上,这个问题是答复...( 另一个问题

rel =nofollow> CORS 。

文件的ID存储在变量< theID >;这个ID是唯一的,只要文件没有被删除就不会改变(a
$ b

  • 第一个 gapi.client.request 检索downloadUrl属性;返回的值是一个短暂的值;
  • 然后, callback:function 发送一个认证请求以检索该文件感谢它的< downloadUrl>;

  • 赢家是......通过返回onreadystatechange = function(theProgressEvent) / li>


  gapi.client .request({
'path':'/ drive / v2 / files /'+ theID,
'method':'GET',
callback:function(theResponseJS,theResponseTXT){
var myToken = gapi.auth.getToken();
var myXHR = new XMLHttpRequest();
myXHR.open('GET',theResponseJS.downloadUrl,true);
myXHR .setRequestHeader('Authorization','Bearer'+ myToken.access_token);
myXHR.onreadystatechange = function(theProgressEvent){
if(myXHR.readyState == 4){
// 1 =连接正常,2 =收到请求,3 =正在运行, 4 =终止
if(myXHR.status == 200){
// 200 = OK
console.log(myXHR.response);
}
}
}
myXHR.send();
}
});使用Chrome 20.0.1132.57 m和Firefox 14.0.1进行测试的

sup>


Two months ago, in Authorization of Google Drive using JavaScript,
@Nivco was writing about "Handling file content", a very tantalizing:

We could also improve the file content server so that it supports XHR requests.

So, where could "we" find news or roadmaps, information et coetera about this feature?



Actually, this question is a reply... (to another question)

解决方案

Here is a short example using CORS.
The ID of the file is stored in variable <theID> ; this ID is unique and won't change as long as the file is not deleted (a trashed file is not deleted).

  • First gapi.client.request retrieves the downloadUrl property ; the returned value is a short lived value ;
  • Then, the callback: function sends an authenticated request in order to retrieve the contents of the file, thanks to its <downloadUrl> ;
  • And the winner is… returned via onreadystatechange = function( theProgressEvent ).

.

gapi.client.request({
    'path': '/drive/v2/files/'+theID,
    'method': 'GET',
    callback: function ( theResponseJS, theResponseTXT ) {
        var myToken = gapi.auth.getToken();
        var myXHR   = new XMLHttpRequest();
        myXHR.open('GET', theResponseJS.downloadUrl, true );
        myXHR.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token );
        myXHR.onreadystatechange = function( theProgressEvent ) {
            if (myXHR.readyState == 4) {
//          1=connection ok, 2=Request received, 3=running, 4=terminated
                if ( myXHR.status == 200 ) {
//              200=OK
                    console.log( myXHR.response );
                }
            }
        }
        myXHR.send();
    }
});

tested with "Chrome 20.0.1132.57 m" and "Firefox 14.0.1"

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

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