03 · Applications
Multipart file upload
Accept multipart/form-data uploads, read the file as a Web Standards File object, persist to disk. Works identically across runtimes.
Code
file-upload.js
const { WebApp } = require(class="tk-str">39;@hyperbridge/forge/server39;); const fs = require(class="tk-str">39;fs/promises39;); const app = new WebApp(); app.post(class="tk-str">39;/upload39;, async (c) => { const form = await c.req.formData(); const file = form.get(class="tk-str">39;file39;); if (!(file instanceof File)) { return c.json({ error: class="tk-str">39;no file39; }, 400); } const buf = Buffer.from(await file.arrayBuffer()); await fs.writeFile(class="tk-str">`/tmp/${file.name}`, buf); return c.json({ name: file.name, size: file.size, type: file.type, }); }); app.listen(3000);
How it works
c.req.formData() returns a standard FormData instance. Files come through as File objects with name, size, type, and arrayBuffer() — the same API used by browsers.
On Cloudflare Workers, swap fs.writeFile for c.env.MY_R2.put(file.name, buf) to land it in R2 storage.
The body is fully cached after the first read, so you can read the same upload as formData() and inspect arrayBuffer() later without errors.
Try it
Quickstart
curl -F file=@photo.jpg http://localhost:3000/upload
Related modules
This is HBForge's port of Hono's example. Read the original at hono.dev/examples/file-upload.