How to save a file recived using parseBody() method. #2237
Unanswered
vinay-khatri
asked this question in
Q&A
Replies: 2 comments 1 reply
-
cc: @sor4chi Can you handle this question? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi, @vinay-khatri import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { writeFile } from "fs/promises";
const UPLOAD_DIR = "uploads";
const app = new Hono();
app.post("/api/image", async (c) => {
const body = await c.req.parseBody();
const image = body.image;
// === validation ===
if (image === undefined) {
return c.json({ error: "Image is required" }, 400);
}
if (!(image instanceof File)) {
return c.json({ error: "Invalid request" }, 400);
}
// === save image ===
const path = `${UPLOAD_DIR}/${image.name}`;
const buffer = await image.arrayBuffer();
await writeFile(path, Buffer.from(buffer));
return c.json({ message: "Image uploaded" });
});
serve(app); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Does anyone know how to save a file to local disk recieved in c.req.bodyParse() method?
Beta Was this translation helpful? Give feedback.
All reactions