I want to download multiple files in a zip file. When the zip file downloads and I try opening it, I get the following error on macos 'unable to expand test into downloads (Error 1 - Operation not permitted)'.
Below is the code I use to generate zip files using jszip
var pages = $pagesComponentsDownloadSavetoFileinput[0].files;
var zip = new JSZip();
for(let i = 0; i < pages.length; i++) {
let reader = new FileReader();
reader.onload = function() {
zip.file(pages[i].name, pages[i].content);
console.log(zip) // returns proper file name and content objects
}
reader.readAsText(pages[i]);
}
window.setTimeout(function() { // setTimeout fixes the issue
zip.generateAsync({type:"blob"}).then(function(blob) {
console.log(blob); // returns Blob(22) {size: 22, type: "application/zip"} with no content
saveAs(blob, "TESTING.zip");
}, function (err) {
console.log(err)
});
}, 5000);
Edit: I'm using filesaver.js to save the zip file
EDIT2: The downloaded zip file is only 22 bytes large and doesn't contain the content added in the for loop.
EDIT 3: loggining 'blob' in generateAsync does not return the proper content. While logging zip in the for loop does.
EDIT 4: I added setTimeout of 5 seconds to zip.generateAsync and it fixed the issue. All files download after 5 seconds. It seems like zip.generateAsync triggers before the zip files are added in the for loop. Is there a solution that fixes this without using unreliable settimeout, perhaps a fallback once all files are added in the for loop?