Eduardo La Hoz Miranda
Eduardo La Hoz Miranda

Reputation: 2060

Adding mp3s into zip using jszip

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;

Upvotes: 2

Views: 1755

Answers (1)

Eduardo La Hoz Miranda
Eduardo La Hoz Miranda

Reputation: 2060

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

Upvotes: 1

Related Questions