Good morning, I am using JSZip to create a zip, I have this code:
for (var i = 0, f; f = content[i]; i++) {
var zip = new JSZip();
zip.file(f.name, f);
zip.generateAsync({type:"blob", compression:"DEFLATE", compressionOptions: { level: 9 }})
.then(function(content) {
var item = {
'type' : content.type,
'size' : content.size,
//'name' : ¿content.name?
}
});
}
I do not want to download the file, I want to upload it to my server, but it does not have an attribute name, it has two attributes: type and size. But I need to set a name to the file.
It does not work if I write 'name' : f.name, because the function is async.
Do someone know how to pass the attributte name to content.
Edit: or, if you can tell me how to make it not async, that will work too.
Thanks for help.
Solved using this:
Use a function that calls the async method, and receive the ZIP and the name of the file.
function makeZip(zip, name) {
name = name.split('.')[0] + ".zip";
zip.generateAsync({type:"blob", compression:"DEFLATE", compressionOptions: { level: 9 }})
.then(function(content) {
// see FileSaver.js
console.log(content)
saveAs(content, name)
var item = {
'name': name,
'type': content.type,
'size': content.size,
'guid': generatorguid()
};
});
}
And then call it from the for:
for (var i = 0, f; f = content[i]; i++) {
var fZip = new JSZip();
fZip.file(f.name, f);
makeZip(fZip, f.name);
}