Search code examples
javascriptpostxmlhttprequestzipjszip

xhr Post with JSZip Javascript / JSZip upgrade


I'm trying to 'post' and xhr request using a zip file but I'm having trouble getting the zip in the right format. I found a post that showed a similar request using this:

var zip = new JSZip(); // create the jszip zip
var input = $("#image")[0]; // Get the image from dom (image is an input button)
zip.file("test.png", input.files[0], {base64: true}); // Add uploaded image to zip

var content = zip.generate({type:"blob"}); // Format zip to blob

//prepare file for api call
var data = new FormData();
data.append("files", content, "Test.zip");

First things first, zip.generate({type:"blob"}); has been deprecated. The upgrade guide states:

// 2.x
zip.generate();
// 3.x
zip.generateAsync({type:"uint8array"})
.then(function (content) {
    // use content
});

I don't understand what "use content" is. And if I just leave that function blank the code won't run. I'd list the error but I'm using SAP WEB IDE and it simply won't run it, it doesn't display an error.

How can I format the zip to work for an xhr request?


Useful links:


Solution

  • why don't you try this way

    zip.generateAsync({type:"blob"}).then(function(content) {
      var data = new FormData();
      data.append("files", content, "Test.zip");
    });
    

    check it here https://github.com/Stuk/jszip