-
-
Save c4software/981661f1f826ad34c2a5dc11070add0f to your computer and use it in GitHub Desktop.
Download multiple files then compress to one zip file using JSZip & JSZip-utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var zip = new JSZip(); | |
var count = 0; | |
var zipFilename = "zipFilename.zip"; | |
var urls = [ | |
'http://image-url-1', | |
'http://image-url-2', | |
'http://image-url-3' | |
]; | |
urls.forEach(function(url){ | |
var filename = "filename"; | |
// loading a file and add it in a zip file | |
JSZipUtils.getBinaryContent(url, function (err, data) { | |
if(err) { | |
throw err; // or handle the error | |
} | |
zip.file(filename, data, {binary:true}); | |
count++; | |
if (count == urls.length) { | |
zip.generateAsync({type:'blob'}).then(function(content) { | |
saveAs(content, zipFilename); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1
I have also struggled with file name being redundant and i ended up with a zip with one file, i suggest adding something like
self.crypto.randomUUID();
https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID to ensure uniqueness!