I'm making a file uploading API on Svelte. You send images via form-data and the API saves it locally.
This is what my API looks like:
export const post: RequestHandler = async ({ request }) => {
const data = await request.formData();
console.log(data)
return { status: 200, body: "h"};
};
The request's body:
----------------------------930945094336727102920559
Content-Disposition: form-data; name="image"; filename="index.jpg"
Content-Type: image/jpeg
����
basically this, I can not paste the rest
I just get FormData {} on the console
How can I save this image locally using node:fs or something similar
FormData has to be read and will not automatically be serialized. From the body it looks like the input already has a suitable name and is posted correctly, so you should be able to just read that as a File.
e.g.
import * as fs from 'fs/promises';
export const post: RequestHandler = async ({ request }) => {
const data = await request.formData();
const file = data.get('image') as File;
await fs.writeFile(`D:/${file.name}`, file.stream());
return { status: 200, body: "h"};
};