Google Drive resumable upload with javascript

后端 未结 2 1753
挽巷
挽巷 2021-01-07 01:52

I\'m trying to upload files to Google Drive using Google APIs Client Library for JavaScript and resumable upload type.

I authenticate and get the upload URI successf

相关标签:
2条回答
  • 2021-01-07 02:17

    Blob types are a hot topic for XMLHttpRequest implementations and they are not truly mature. I'd recommend you to stick with base64 encoding. Google's JavaScript client lib doesn't support resumable uploads because it's very unlikely that a client side browser app uploads very large files directly to Google Drive.

    0 讨论(0)
  • 2021-01-07 02:27

    What works

    To upload a binary blob, use github/googleapi's cors-upload-sample or use my gist fork, UploaderForGoogleDrive, which will grab access_token out of the gapi client for you.

    Here is an ugly mixture of Promise and callback code that works for me. As a prerequisite, gapi,UploaderForGoogleDrive, JSZip need to be loaded via <script> tags. The snippet also omits gapi initialization and the API secrets, which are also necessary.

    function bigCSV(){  // makes a string for a 300k row CSV file
        const rows = new Array(300*1000).fill('').map((v,j)=>{
          return [j,2*j,j*j,Math.random(),Math.random()].join(',');
        });
        return rows.join("\n");
    }
    
    function bigZip(){  // makes a ZIP file blob, about 8MB
        const zip = new window.JSZip();
        zip.folder("A").file("big.csv", bigCSV());
        return zip.generateAsync({type:"blob", compression:"DEFLATE"});
        // returns Promise<blob>
    }
    
    function upload2(zipcontent){
       'use strict';
        const parent = 'root';
        const spaces = 'drive';
        const metadata = {
          name: 'testUpload2H.zip',
          mimeType: 'application/zip',
          parents: [parent]
        };
        const uploader = new window.UploaderForGoogleDrive({
          file: zipcontent,
          metadata: metadata,
          params: {
            spaces,
            fields: 'id,name,mimeType,md5Checksum,size'
          },
          onProgress: function(x){
             console.log("upload progress:",Math.floor(100*x.loaded/x.total));
          },
          onComplete: function(x){
            if (typeof(x)==='string') x = JSON.parse(x);
            // do something with the file metadata in x
            console.log("upload complete: ");
          },
          onError: function(e){ console.log("upload error: ",e); }
        });
        uploader.upload();
    }
    
    function uploadZipFile(){
        'use strict';
        (bigZip()
          .then(upload2)
        );
    }
    

    What doesn't work

    As of Nov 2017, uploading a binary blob with the gapi.client.request call is not going to work, because of an issue where gapi removes the PUT payload

    I've also tried using base64 with gapi, which works. but deposits base64 files, not true binaries; and the fetch API in cors mode, which half-worked but produced CORS-related errors and response hiding, at least for me.

    0 讨论(0)
提交回复
热议问题