使用Javascript API进行Google云端硬盘可恢复文件上传问题

时间:2016-03-10 17:07:03

标签: javascript google-drive-api

我正在尝试将Google驱动器可恢复文件上传/更新与我的应用程序集成。但是当我更新文件时,文件正在以编码格式更新,它没有采用实际内容。编码格式适用于multiplepart uploadType,但相同的内容不适用于Resumable upload。请找到以下详细信息

步骤1:启动可恢复会话

function uploadFile(fileData) {

    var accessToken = 'ya29.nwI5Em6UnYGHvVzVx7lBk5tD-xzFl4_JG3_c-_t4FJ3owll_8i_rL5M17LFV6VlF7QE';

    const boundary = '-------314159265358979323846';
    const delimiter = "\r\n--" + boundary + "\r\n";
    const close_delim = "\r\n--" + boundary + "--";

   var contentType = fileData.type || 'application/octet-stream';
   var metadata = {
            'name': fileData.name,
            'mimeType': contentType,
            'Content-Type': contentType,
            'Content-Length': fileData.size
        };

   var request = gapi.client.request({
        'path' : 'upload/drive/v3/files',
        'method' : 'POST',
        'params' : {'uploadType':'resumable'},
        'headers' : {
          'X-Upload-Content-Type' : fileData.type,
          'Content-Type': 'application/json; charset=UTF-8',
          'Authorization': 'Bearer ' + accessToken,
        },
        'body' : metadata
    });

   request.execute(function(resp, raw_resp) {
      var locationUrl =   JSON.parse(raw_resp).gapiRequest.data.headers.location;
      console.log(locationUrl);
      uploadToLocationUrl(locationUrl, fileData);
   });
}

到此为止,我正在获取位置网址,然后调用上传文件的功能。

步骤2:可恢复的会话发起请求

     function uploadToLocationUrl(locationUrl, fileData)
       {
            var reader = new FileReader();
            reader.readAsBinaryString(fileData);
            reader.onload = function (e) {
            var contentType = fileData.type || 'application/octet-stream';
            var metadata = {
                'name': fileData.name,
                'mimeType': contentType,
                'Content-Type': contentType,
                'Content-Length': fileData.size
            };

              var base64Data = btoa(reader.result);
              var multipartRequestBody =
                  delimiter +
                  'Content-Type: application/json\r\n\r\n' +
                  JSON.stringify(metadata) +
                  delimiter +
                  'Content-Type: ' + contentType + '\r\n' +
                  'Content-Transfer-Encoding: base64\r\n' +
                  '\r\n' +
                  base64Data +
                  close_delim;

           var requestPost = gapi.client.request({
                'path' : locationUrl,
                'method' : 'PUT',
                'headers' : {
                  'X-Upload-Content-Length' : fileData.size
                },
                'body' : multipartRequestBody
              });
            console.log(requestPost);

            requestPost.execute(function(resp, raw_resp) {
              console.log(resp);
            });
         }
}

结果:更新了谷歌驱动器中的文件

---------314159265358979323846
Content-Type: application/json

{"name":"api.txt","mimeType":"text/plain"}
---------314159265358979323846
Content-Type: text/plain
Content-Transfer-Encoding: base64

MSkgTmVlZCBhbiBhcGkgd2hpY2ggd2lsbCByZXR1cm4gYWxsIGxlYWRzIGVtYWlsIGlkLg0KMikgTmVlZCBhbiBhcGkgdG8gY29udmVydCBtdWx0aXBsZSBjb250YWN0IGludG8gbGVhZC4NCjMpIE5lZWQgYW4gYXBpIGZvciBnb29nbGUgc2lnbiBpbi4vLyBkb24ndCBkaXNjdXNzIGFib3V0IHRoaXMgb25lIG5vdywgZmlyc3Qgd2Ugd2lsbCBkaXNjdXNzIGFib3V0IHRoaXMgQVBJLg==
---------314159265358979323846--

1 个答案:

答案 0 :(得分:0)

对不起延迟回答,

您只需要更改 uploadToLocationUrl 功能代码

在下面找到更新的代码,

tasks.withType(JavaExec) {
    jvmArgs = ['-Xms1g', '-Xmx16g']
}

你不需要再次发送元数据,只需字节数组就可以了。