D
Deno

help

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

AAttacler/Bart1/24/2023
Could someone give me an example for this?
IioB1/24/2023
can you explain what you’re trying to do?
IioB1/24/2023
I think this might be a https://xyproblem.info
The XY Problem
Asking about your attempted solution rather than your actual problem
AAttacler/Bart1/24/2023
I already found it, i basicly have to transform the base64 string into a file and then just provide it in the new Response 🙂
IioB1/24/2023
Could you share code for your solution? This may be useful to people in the future looking for how to do this as well...
AAttacler/Bart1/24/2023
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});
}
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"));
return new Response(dataURLtoFile(base64Image,"file.name"));
This will return the file to the visitor of your deno deploy project
IioB1/24/2023
cool! thanks for sharing!
AAttacler/Bart1/24/2023
no problem! the function dataURLtoFile comes somewhere from stackoverflow

Looking for more? Join the community!

Recommended Posts
How to disable deno linter for whole project directory?I'm using deno with mixed project folders, some use Vue and shouldn't be linted by deno. How do i diHow can I get `npm` in the path for Deno.run()?I'm writing a script to automate a tedious npm task that I run frequently. When I use `Deno.run()` iUse Deno.run() to communicate with ArduinoHey I'm trying to communicate via USB serial with my Arduino. My goal is to have the result I get viBest place to host server side Deno AppI have a server side app I want to host in a specific region and I only want 1 instance of it runninNPM Import missing typesPlaying around with some deno code and testing a npm import, the original package has types (redaxioUpdate value by reference without duplicating the dataIm exploring the world of FFI, porting some C applications I have to TypeScript, with the help of Dehow to hash very large filesWe have deprecated the old std/hash code in favor of the crypto module, but I do not know what the sxstate - no types importedimport * as xstate from "https://deno.land/x/xstate@xstate@4.35.2/src/index.ts"; file is cached, yetexample of kysely running on deno deploy?Deno Deploy awesomely can connect to postgres directly from the edge https://deno.com/blog/deploy-poUpdating digest with web cryptoFor some context, I'm updating some code that was importing from `std/hash` (specifically `std/hash/What would be the best third party module implementing `deflate()`?Currently using `npm:pako` but I'd like to avoid npm specifiers (modules in general).Pass string to struct FFIQuick question about Struct-Value feature, what would be the best way to pass `*const u8` (or stringdeno blog without html sanitizationI want to be able to javascript Canvas API things in my blog - surely this is possible?Killing subprocess not workingAnyone know what I'm doing wrong when trying to kill a process? I have a process start another procPossible to check of a file is being run as worker?```ts if (import.meta.main) { main() } ``` is there a way to disable calling `main()` if the file Deno linter is telling me that writeAll from streams conversion is deprecatedIs import { writeAll } from "https://deno.land/std@0.171.0/streams/conversion.ts" deprecated?How to get Deno's default compilerOptions value?I want to add more compilerOptions.lib options like this: ``` "compilerOptions": { "jsx": "rea