Pocky
Pocky

Reputation: 75

How to save image to zipzap

I am doing a project with zipzap and want to zip a image file in zipzap. But the image doesnt show correctly after zip ,the reason is the image data was get from XMLHttpRequest and the data from image seems not encode in correct way.

My code:

function requestImgPart(url) {

  var request = new XMLHttpRequest();



  request.open("GET", url, true);
  request.setRequestHeader('Accept', 'image/*');
  request.onload = onload;
  request.onerror = onerror;
  request.send();

  function onload() {
    if (request.status === 200) {
       var zip= new JSZip();
       zip.file("1.png",request.responseText);
       zip.saveAs("presentations.zip");
    } 
  }

  function onerror() {

  }

}

The url is something like http://upload.wikimedia.org/wikipedia/commons/d/d6/MicroQR_Example.png

Does any one know what is wrong inside my code

Upvotes: 0

Views: 128

Answers (1)

David Duponchel
David Duponchel

Reputation: 4059

This comes from request.responseText : the browser tries to interpret the result as a string and decode it from its charset (but it's not a text and you get corrupted data).

You can ask an ArrayBuffer with request.responseType = "arraybuffer";, the data will be in the response attribute : zip.file("1.png",request.response);.

If you need IE 6-9 support, the responseType above won't work, see here for more informations.

Upvotes: 1

Related Questions