How to uploading images from frontend to Deno.serve()
Hey,
can someone please give me a vanilla code example on how to upload files from my frontend application to the server that uses
Deno.serve()
.
I know how to upload the image in the frontend and send it to the Deno backend using a POST request, but how can I work with this image on the server side?
Currently my backend is set up without any framework.
Thanks!6 Replies
This is currently my code:
@Deno AI Helper
Sure, I'd be happy to help. Could you please provide me with the question you'd like me to answer?
Hey,
can you please give me a vanilla code example on how to upload files from my frontend application to the server that uses
Deno.serve()
.
I know how to upload the image in the frontend and send it to the Deno backend using a POST request, but how can I work with this image on the server side?
Currently my backend is set up without any framework.
Thanks!Sure, I can help with that.
On the server side, you can use the
fetch
API to receive the uploaded file. The fetch
API is part of the global scope in Deno, and it's compatible with the fetch
API in the browser.
When you receive a fetch
event, you can call event.request.formData()
to parse the incoming form data. This will return a FormData
object, which you can use to access the uploaded files.
Here's a basic example of how you can do this:
In this example, we're listening for fetch
events. When we receive a POST
request, we parse the form data and get the uploaded file. We then log the file name and read the file content. Finally, we write the file content to a new file in the current directory.
Please note that you'll need to run your script with the --unstable
and --allow-read
flags because the FormData
API and the Deno.writeFile
API are currently unstable and require read access respectively.
Also, please replace "image"
with the actual field name you're using to upload the file in your frontend application.
I hope this helps! Let me know if you have any other questions. π¦I'm not sure what exactly you mean by "work with this image", but you can manipulate it as a blob:
You could also manipulate it as a
Uint8Array
Okay thanks ππ»
I donβt want to manipulate it. I just want to save the image on the server side as a for example png file