I have a table with URLs and Images (We can assume that this table will have less than 10 rows):
Now I'm trying to download the images from the table in a loop:
for (i = 0; i < ImageTable.data.length; i++) {
utils.downloadFile({base64Binary: ImageTable.data[i].Image }, "" + i, ImageTable.data[i].FILEEXTENSION);
}
However when I open the files, the content is {}
instead of the image binaries.
What is the best way to download the files? Are there any examples for that?
not ImageTable.data[i].Image is not a base64 string. You must use query to fetch url to get the base64 string.
Here is some related topic for your reference.
I am testing out interacting with a file conversion api. I return the completed file conversions as urls. I want to do some magic that pulls in the data from the urls and then converts it into a base64 url so that I can do other things with it. I am having great difficulty figuring out how to get the data at the end of the urls into retool. It seems I can place it into an image component or I can open the urls and the file will download but I can't get it into retool as a variable or somethi…
You also can use jszip
library to download them in a zip file.
1 Like
Thanks Ansown, with your pointer I was able to come up with blow following solution.
Note: Calling the Rest Query for each image is a slow and cumbersome; it would be great to have access to the raw image data in the table directly but that might be a browser limitation...
The REST Query is:
And the corresponding javascript is:
let zip = new JSZip();
var i = 0;
var msg = "";
for (i = 0; i < ImageTable.data.length; i++) {
var fileName = ImageTable.data[i].ID + '.' + ImageTable.data[i].FILEEXTENSION;
msg = "Writing Image # " + i + ": " + fileName + " (" + ImageTable.data[i].PATH + ")";
console.log(msg);
try {
var img = await Promise.resolve(QueryImage.trigger({
additionalScope: { i : i } // This is where we override the `i` variable
}));
} catch (e) {
console.error(e);
utils.showNotification({title: "Error", description: e, notificationType: "Error"})
}
//var img = await utils.getDataByObjectURL("blob:" + ImageTable.data[i].PATH);
//console.log(img);
//uils.downloadFile({base64Binary: img.base64Binary }, "" + i, ImageTable.data[i].FILEEXTENSION);
var binaryImg;
try {
binaryImg = atob(img.base64Binary);
} catch(e) {
binaryImg = img.base64Binary;
console.log("WARNING: Image # " + i + ": " + fileName + " (" + ImageTable.data[i].PATH + ") is NOT base64 encoded!!!")
}
zip.file(fileName, binaryImg, {binary: true});
//if (i===10) {break}
}
console.log("Done writing " + i + " out of " + ImageTable.data.length + " Images");
zip.generateAsync({
type:"base64",
compression: "DEFLATE",
compressionOptions: {
level: 9
}}).then(function (base64) {
utils.downloadFile({base64Binary: base64}, 'myzip', "zip")
}, function (err) {
utils.showNotification({title: "Error", description: err, notificationType: "Error"})
});
1 Like