question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Google Drive Resumable File Upload issue with Javascript API

See original GitHub issue

I am trying to integrate Google drive resumable file upload/update with my application. But when i update the file, file is updating in encoded format it is not taking the actual content. Encoded format is working for multiplepart uploadType but same content is not working for Resumable upload. Please find the below details

Step 1 : Start the resumable session

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

Upto here it’s fine I am getting Location Url and then calling a function to upload the file.

Step 2 : Resumable session initiation request

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

Result : Updated file in google drive

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

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

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

Thank you.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jmdobrycommented, Dec 28, 2016

@anurag-itsolvs All the code you’ve shown is browser code, while this repository is for the server-side Node.js google-api-nodejs-client.

0reactions
JustinBeckwithcommented, Apr 15, 2018

Greetings folks! This library doesn’t currently support resumable uploads. This is on the backlog, and being tracked in #276. Lets track it over there 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Drive Resumable File Upload issue with Javascript API
Upto here it's fine I am getting Location Url and then calling a function to upload the file. Step 2 : Resumable session...
Read more >
Upload file data | Google Drive
The Drive API lets you upload file data when you create or update a File . For information about how to create a...
Read more >
Google Drive API, Resumable upload — Using NodeJS
In this article, I will demonstrate the process of performing a google drive resumable file upload using node js.
Read more >
Resumable uploads - writing large files to Drive with Apps Script
// this file would be too big, so need to use drive api var name = 'bigQueryJeopardy.txt'; var status = cUseful.FetchUtils .setService(UrlFetchApp) ....
Read more >
Simple Script of Resumable Upload with Google Drive API for ...
Download data from URL. · Create the session for the resumable upload. · Retrieve the downloaded data from the stream and convert it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found