2

everyone. Trying to unsuccesfully add mp3s into my zip file using the wonderful JSZIP lib. Right now, it only created the zip file with the correct file name, but the mp3 is always blank.

This is the code I have so far:

//init
var zip = new JSZip();

//add an mp3 titled "any other way" and decode binary to base64
zip.file("any other way.mp3", btoa("absolutepath/to/my/file/any_other_way.mp3"), {base64: true});

//generate zip
var content = zip.generate();

//download zip
location.href="data:application/zip;base64,"+content;
  • 1
    are you sure btoa will get the file from your PC and encode it? As far as I know, it will only encode the string parameter. If you want to get the real file content you have to use an file input. https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications – Luizgrs Sep 29 '14 at 17:19
  • Geez, that might be it. Will let you know if it worked. – Eduardo La Hoz Miranda Sep 29 '14 at 18:00

1 Answers1

1

So, I ended up including another js file with the JSZIP utils in my project and called the following method and it downloaded fine on Chrome. However, if you want to make it work on IE and safari you will have to implement Downloadify (http://stuk.github.io/jszip/documentation/howto/write_zip.html#toc_3):

  // loading a file and add it in a zip file
  JSZipUtils.getBinaryContent("path/to/audio.mp3", function (err, data) {
      if(err) {
      throw err; // or handle the error
      }
      var zip = new JSZip();
      zip.file("audio.mp3", data, {binary:true});
  });

http://stuk.github.io/jszip-utils/documentation/api/getbinarycontent.html

Also, here is a link to the issue: https://github.com/Stuk/jszip/issues/176#issuecomment-57266207