4

Have you got a google drive resumable upload example from javascript ? I've read instructions at : https://developers.google.com/drive/manage-uploads#resumable And I've tried do to a resumable upload like that :

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

This request succeeded and I get an upload URL. But when I try to upload content using this URL like that :

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

The request return an error (status 400 Bad request).

Can anybody give me an example of a correct request to Drive API for resumable upload with Javascript, please?

  • Possible duplicate of [Google Drive resumable upload with javascript](https://stackoverflow.com/questions/17918995/google-drive-resumable-upload-with-javascript) – Paul Nov 04 '17 at 04:54

0 Answers0