NextJS API how to send files?
Unanswered
Saltwater Crocodile posted this in #help-forum
Saltwater CrocodileOP
Hi there, I'm sending some files from the frontend to an NextJS API endpoint which then sends the files to a new API, but what is the best way to do this?
Anyone who could point me in the right direction here?
Anyone who could point me in the right direction here?
1 Reply
Gull Terrier
in your form use the <input type="file" name="file"/>
Here's a lazy example:
Bind the <form onSubmit={onSubmit}>....
async function onSubmit(e) {
const formData = new FormData(e.target); // i think, can't remember exactly.
const resp = await fetch(url, {
method: "POST",
body: formData,
});
on your api you should be able to just get the buffer doing something like this:
const data = await request.formData();
const file: File | null = data.get("file") as unknown as File;
const bytes = await file.arrayBuffer();
const buffer = Buffer.from(bytes);
Here's a lazy example:
Bind the <form onSubmit={onSubmit}>....
async function onSubmit(e) {
const formData = new FormData(e.target); // i think, can't remember exactly.
const resp = await fetch(url, {
method: "POST",
body: formData,
});
on your api you should be able to just get the buffer doing something like this:
const data = await request.formData();
const file: File | null = data.get("file") as unknown as File;
const bytes = await file.arrayBuffer();
const buffer = Buffer.from(bytes);