I tried searching a lot for this issue but did not get anything that helps me.
I am trying to download a bunch of pdf files from Strapi using URL and after downloading every pdf I am pushing it into a folder to create a zip.
My issue is while downloading, few of the files are not able to push in zip folder because of large file size. Basically, zip creation is not waiting to complete the download.
Here is my code, I am using map() to get URLs and using blob() to download it and then used zip to save it in the zip folder.
const downloadPdf = async (newData: any) => {
return new Promise(async (resolve) => {
let PolicyDocData: any;
[...some operation...]
if (newData.laws) {
if (newData.laws.description[0]) {
[...some operation to create an array for URL...]
let lawDoc = Array.from(new Set(lawDocArray));
lawDoc.map(async (val: any, index: any) => {
const lawUrl = val.url.slice(1);
const DocUrl = serverUrl + lawUrl;
let download = await fetch(DocUrl, {
method: "GET",
}).then((response) => response.blob()).then((response) => {
let fileType = ".pdf";
const timeStamp = Math.floor(Date.now() / 1000);
const fileName = val.name;
let docFileName = fileName + "_" + timeStamp;
zip.folder("Compliance/Laws").file(docFileName + fileType, response);
})
if (lawDoc.length - 1 === index) {
resolve(Contents);
}
})
}
}
}).then((Contents: any) => {
if (Contents) {
Packer.toBlob(PolicyDocumentCreator(Contents)).then((blob) => {
zip.generateAsync({ type: "blob" }).then(function (content: any) {
saveAs(content, folderName ? folderName : complianceName + ".zip");
setLoading(false);
})
});
}
});
};
I tried to solve it using setTimeout() but this is not a feasible solution.