I have the pdf files stored in retool storage and I made them public URL. In the retool app table, I'm trying to download the file from the button click on the custom column. Depending on which row button is clicked, the file name varies. I'm trying to write the JS script to download this file triggered from the button click.
First: I tried with below JS code and it downloads dummy pdf(corrupted).
const fileUrl = currentRow.payslip_url;
if (!fileUrl) {
console.error("Payslip URL is missing");
return;
}
utils.downloadFile(fileUrl, payslip_${currentRow.employee_id}_${currentRow.pay_period}.pdf);
Second: I tried to create a resource query and pointed to retool storage with the action "download a file", and the file name (Fx), there is no parameter to pass to this function so I can't pass the dynamic file name.
Third: Also tried the script with link instead of utils.downloadFile. Issue: Fetch does not work.
const fileUrl = currentRow.payslip_url;
if (!fileUrl) {
console.error("Payslip URL is missing");
return;
}
fetch(fileUrl)
.then(response => response.blob()) // Convert response to a binary blob
.then(blob => {
const url = window.URL.createObjectURL(blob); // Create a URL for the blob
const a = document.createElement("a");
a.href = url;
a.download = payslip_${currentRow.employee_id}_${currentRow.pay_period}.pdf
; // Set filename
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url); // Clean up URL object
})
.catch(error => console.error("Error downloading the PDF:", error));