#How can i return a base64 as file on Deno Deploy?

8 messages · Page 1 of 1 (latest)

broken marsh
#

Could someone give me an example for this?

rocky aspen
#

can you explain what you’re trying to do?

broken marsh
#

I already found it, i basicly have to transform the base64 string into a file and then just provide it in the new Response 🙂

rocky aspen
#

Could you share code for your solution? This may be useful to people in the future looking for how to do this as well...

broken marsh
#

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

rocky aspen
#

cool! thanks for sharing!

broken marsh