javascript - Track progress of file upload to google cloud storage via js? -


i'm wondering if possible track progress of file upload google cloud storage when using javascript client? have progress bar show user on how of file has been uploaded far.

i'm using same upload code google gives on example page file upload.

instead of using gapi.client.request api provided js library instead created xmlhttprequest , appended event listener event progress. seen following:

      const boundary = '-------314159265358979323846';       const delimiter = "\r\n--" + boundary + "\r\n";       const close_delim = "\r\n--" + boundary + "--";        var reader = new filereader();       reader.readasbinarystring(filedata);       reader.onload = function(e) {         var contenttype = filedata.type || 'application/octet-stream';         var metadata = {           'name': 'testing',           'mimetype': contenttype         };          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;          gapi.client.request()         var xhr = new xmlhttprequest();          xhr.open('post', 'https://www.googleapis.com/upload/storage/v1/b/bucket/o?uploadtype=multipart&key=apikey&alt=json', true);          xhr.setrequestheader('content-type', 'multipart/mixed; boundary="' + boundary + '"');         xhr.setrequestheader('authorization', 'bearer ' + gapi.auth.gettoken().access_token);          xhr.upload.addeventlistener("progress", function(e) {           var pc = parsefloat(e.loaded / e.total * 100).tofixed(2);         }, false);          xhr.onreadystatechange = function(e) {           if (xhr.readystate == 4) {             console.log(xhr.responsetext);           }         };          xhr.send(multipartrequestbody); 

attribution: majority of code taken google's js library addition being event listener listening progress of upload.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -