Created
December 6, 2015 23:22
-
-
Save noelvo/4502eea719f83270c8e9 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) { | |
var zipFile = zip.generate({type: "blob"}); | |
saveAs(zipFile, zipFilename); | |
} | |
}); | |
}); |
Hey Thank you all below code is working fine for me
var urls = [
"images/20170420_145140.jpg",
"images/20170503_142841.jpg",
"images/20170503_084035.jpg"];
download() {
urls.forEach(function (url) {
JSZipUtils.getBinaryContent(url, function (err, data) {
if (err) {
throw err; // or handle the error
}
try {
zip.file(count + ".jpg", data, { binary: true });
count++;
if (count == urls.length) {
zip.generateAsync({ type: "blob" }).then(function (content) {
FileSaver.saveAs(content, zipFilename);
});
}
} catch (e) {
console.log("errorrr...k", e)
}
});
});
}
The mistake I did is zip.file("image", data, { binary: true });
I am passing three URLs but adding name static so it is downloading one file. after changing the name dynamically it is working.
I just created a new App using the multiple-file-downloader
Here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello. I am using the version3 code to download a bunch of pdf from a database. I am using some big paths and those paths are creating the whole folder heirarchy on the zip. Is there a way to just put them on the top of the zip and not creating useless folders?