Got it! Thanks for that context.
To start, it might be easier if you pass {{ fileInput1.value[0] }}
to your upload query. File inputs typically store their values in an array so accessing it with [0]
will make sure you're uploading just the base64 value and not an array containing the base64 string.
(Replace date
in the image above with nome
).
Since Retool is uploading a base64 string to your MySQL DB but the column type is blob
there's some conversion that goes on in the background. We can undo this in your get query with something like:
select nome, convert(file using utf8) as file from Menu;
From there you should be able to use
utils.downloadFile({base64Binary: table1.selectedRow.data.file}, table1.selectedRow.data.nome,'pdf');
If it's important that you upload specifically an array of file data you'll instead want to use:
utils.downloadFile({base64Binary: JSON.parse(table1.selectedRow.data.file)[0]}, table1.selectedRow.data.nome,'pdf');
Alternatively, there's a more complex JavaScript solution where you both convert and download the file using the same script:
const bufferData = table.selectedRow.data.file.data;
const binArrayFile = new Uint8Array(bufferData, 1, bufferData.length / 2);
let str = "";
for (let i = 0; i < binArrayFile.length; i++) {
str += String.fromCharCode(parseInt(binArrayFile[i]));
}
//use this when uploading with {{ fileInput1.value[0] }}
utils.downloadFile({base64Binary: str}, table.selectedRow.data.nome, "pdf");
//use this when uploading with {{ fileInput1.value }}
const base64File = JSON.parse(str)[0];
utils.downloadFile({base64Binary: base64File}, table.selectedRow.data.nome, "pdf");
Let me know if that helps!