Google Drive API可在Javascript中恢复上传

时间:2013-06-28 08:52:18

标签: javascript google-drive-api

你有一个来自javascript的谷歌驱动器可恢复上传示例吗? 我已阅读说明: https://developers.google.com/drive/manage-uploads#resumable 我已经尝试过像这样的可恢复上传:

    var metadata = {
        'Content-Length': '200',
        'title': "MyFileTitle"
    };

    var multipartRequestBody = JSON.stringify(metadata);
    var request = gapi.client.request({
        'path': '/upload/drive/v2/files',
            'method': 'POST',
        'params': {'uploadType': 'resumable'},
        'headers': {
                'Authorization': 'Bearer ' + acToken, 'Content-Type':         'application/json; charset=UTF-8', 'X-Upload-Content-Type': 'application/vnd.google-        apps.document'
            },
        'body': multipartRequestBody});
            callback = function(ret, raw) {
                var obj = $.parseJSON(raw);
            var uploadUrl = obj.gapiRequest["data"]["headers"]["location"];
        };
            request.execute(callback);

此请求成功,我获得了上传网址。 但是,当我尝试使用此URL上传内容时:

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

      var contentType = 'text/html';

        var metadata = {
          'title': 'MyFileTitle',
          'mimeType': contentType
        };

        var base64Data = btoa(fileToUpload);
        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 request = gapi.client.request({
        'path': uploadUrl.replace('https://www.googleapis.com', ''),
            'method': 'PUT',
            'params': {'uploadType': 'multipart', 'convert': 'true', 'Content-Length':         fileToUpload.length},
            'headers': {
              'Authorization': 'Bearer ' + acToken, 'Content-Type': 'multipart/mixed;         boundary="' + boundary + '"'
            },
            'body': multipartRequestBody});

          callback = function(file) {
            console.log(file)
          };

        request.execute(callback);

请求返回错误(状态400错误请求)。

有人能举例说明使用Javascript对Drive API进行可恢复上传的正确请求吗?

0 个答案:

没有答案