Search code examples
javascriptjquerygoogle-drive-apijszip

Package and download zip to Google Drive test


Fiddle - http://codepen.io/mikethedj4/pen/dodegW

I wanted to see if you can dynamically package a zip file and save it to Google Frive using JSZip and the Google Drive API.

I'm using the "Explicit render" method to start out with.

HTML

<a href="javascript:void(0)" id="render-link">
  Render the Save to Drive button
</a>
<div id="savetodrive-div"></div>

jQuery/JavaScript

$(document).ready(function() {
  function renderSaveToDrive() {
    var zip = new JSZip();
    zip.file("Hello.txt", "Hello World\n");
    var folder = zip.folder("images");
    folder.file("folder.txt", "I'm a file in a new folder");
    var content = zip.generate({type:"blob"});
    // see FileSaver.js
    // saveAs(content, "test.zip");

    // Save it to Google Drive
    gapi.savetodrive.render('savetodrive-div', {
      src: content,
      filename: 'test.zip',
      sitename: 'My Company Name'
    });
  }
  $('#render-link').click(function() {
    renderSaveToDrive();
  });
});

Whatever I try I keep getting, "Failed Download XHR error".


Solution

  • Your best bet would be a Blob URL (with URL.createObjectURL(content)) or a data URI but that doesn't seem possible: Is it possible to use client-side generated blob url to save to Google Drive