Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript blob to image converting with JSZip

var bl = window.URL.createObjectURL(xhr.response)

var zip = new JSZip();
zip.file(bl);

zip.generateAsync({type:"blob"})
.then(function(content) {
    saveAs(content, "example.zip");

}, function(err){
    console.log(err)
})

My XmlHttpRequest got a response of the type 'blob' from an image file. How can I convert the blob image file to an image file (could be .gif, .jpg, .bmp, .jpg-large, etc.) so that I can make a zip file without errors?

like image 849
sawa Avatar asked Sep 04 '26 12:09

sawa


1 Answers

With URL.createObjectURL you get the url of the blob (blob:https://stackoverflow.com/e62c177a-b4b1-4945-8e13-53bb5a3c8f34 for example). JSZip doesn't resolve it but you can use the blob (so xhr.response in your case) directly. As Patrick Evans said in a comment, you also need to give the file name.

var zip = new JSZip();
zip.file("my_file.ext", xhr.response);

zip.generateAsync({type:"blob"})
.then(function(content) {
    saveAs(content, "example.zip");
}, function(err){
    console.log(err)
});
like image 118
David Duponchel Avatar answered Sep 07 '26 19:09

David Duponchel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!