Utilities

File Upload

Handle multipart/form-data uploads.

Upload

Middleware for handling file uploads. It can save files to disk automatically or keep them in memory for processing.

Installation

bun add @bklarjs/upload
npm install @bklarjs/upload

Usage

import { upload } from "@bklarjs/upload";

// Save uploaded files to ./uploads
app.post(
  "/upload",
  (ctx) => {
    const file = ctx.state.files?.avatar;
    return ctx.json({ savedAt: file.path });
  },
  {
    middlewares: [upload({ dest: "./uploads" })],
  }
);

Validation

You can restrict file size and types.

upload({
  maxSize: 5 * 1024 * 1024, // 5MB
  types: ["image/png", "image/jpeg"], // Only images
  randomize: true, // Rename to UUID
});

On this page