user14479722
user14479722

Reputation:

Zipping multiple PDFs and downloading the zip file?

So as the description says: This is my code

 var zip = new JSZip();
    var urls = ["https://www.link.ca/documents/PDF/Quarters.pdf",
        "https://www.link.ca/documents/PDF/Mills.pdf",
        "https://www.link.ca/documents/PDF/Stop.pdf"
    ];
    var count = 0;
    var zipFilename = "Check_This_Out.zip";
    urls.forEach(function (url) {
        debugger;
        var filename = "filename";
        console.log(url);
        JSZipUtils.getBinaryContent(url, function (err, data) {
            if (err) {
                throw err; // or handle the error
            }
            zip.file(url, data, {
                binary: true
            });
            count++;
            if (count == urls.length) {
                zip.generateAsync({
                        type: "blob"
                    })
                    .then(function (blob) {
                        saveAs(blob, zipFilename);
                    });
            }
        });
    });

The problem I'm facing is that when I unzip it, it creates a folder structure like the following:

C:\Users\samuser\Downloads\https_\www.link.ca\documents\PDF

My issue/question is how do I fix it so that when I open the Zip file, I should see the three documents I uploaded.

Upvotes: 1

Views: 1656

Answers (1)

user14479722
user14479722

Reputation:

If someone had the same issue, this is how I resolved it:

var filename = url.replace(/.*\//g, "");
zip.file(filename, data, { binary: true, createFolders: true });

Upvotes: 1

Related Questions