I’m trying to download a set of files (PDFs, though I don’t think that matters), triggered by the click of a button. I’m basically trying to replicate this example , which fetches files from their URL, puts them into a zip file, and downloads it. I’m pretty new to this and am having trouble getting it to work.
I’ve installed the JSZip NPM package, and created the following backend web module:
import jszipApi from 'jszip'
let fileZipper = new jszipApi();
export function saveToZip (filename, urls) {
const folder = fileZipper.folder('allPdfs')
urls.forEach((url)=> {
const blobPromise = fetch(url).then(r => {
if (r.status === 200) return r.blob()
return Promise.reject(new Error(r.statusText))
})
const name = url.substring(url.lastIndexOf('/'))
folder.file(name, blobPromise)
})
fileZipper.generateAsync({type:"blob"})
.then(blob => saveAs(blob, filename))
.catch(e => console.log(e));
}
And on the client side, I have this:
import {saveToZip} from 'backend/jszip'
$w.onReady(function () {
});
export async function printAll_click(event){
var numOfFiles = $w("#dataset1").getTotalCount();
var allFiles = await $w("#dataset1").getItems(0, numOfFiles );
var fileUrls = new Array(numOfFiles);
for (let i= 0 ; i < numOfFiles ; i++) {
var formId = allFiles .items[i].formId;
var name = allFiles.items[i].name;
var email = allFiles.items[i].email;
var phone = allFiles.items[i].phone;
var pdfUrl = await getPdfUrl({formId,name,email,phone})
fileUrls.push(pdfUrl);
}
await saveToZip("allPdfs.zip", fileUrls);
}
The main problem I’m having is that “saveAs” in the web module is not defined. I believe I need FileSaver API for this but it doesn’t seem to be available on Corvid. Also, if I were to run saveAs on the web module, would that actually download the file on the server? Obviously I want it to download to the client.
If it’s possible to just create the zip file in the backend and then send out a download link by email, that’s fine too.
Does anyone have ideas on how to do this?