Skip to content

Instantly share code, notes, and snippets.

@andz89
Forked from c4software/download-multiple-files.js
Created December 22, 2022 14:22
Show Gist options
  • Save andz89/28babba8b07d60ecbeb263fc793ccbec to your computer and use it in GitHub Desktop.
Save andz89/28babba8b07d60ecbeb263fc793ccbec to your computer and use it in GitHub Desktop.
Download multiple files then compress to one zip file using JSZip & JSZip-utils
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