#How can i return a base64 as file on Deno Deploy?
8 messages · Page 1 of 1 (latest)
can you explain what you’re trying to do?
I think this might be a https://xyproblem.info
Asking about your attempted solution rather than your actual problem
I already found it, i basicly have to transform the base64 string into a file and then just provide it in the new Response 🙂
Could you share code for your solution? This may be useful to people in the future looking for how to do this as well...
Sure:
function dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}
The first argument would be your base64 string, then it gets converted to the image using the second parameter as filename.
Then inside of your deploy function you can use:
return new Response(dataURLtoFile(base64Image,"file.name"));
This will return the file to the visitor of your deno deploy project
cool! thanks for sharing!
no problem!