Skip to content

文件上传

🌐 File Upload

你可以使用 multipart/form-data 上传文件。上传的字段可以通过 c.req.parseBody() 获取。

🌐 You can upload files with multipart/form-data. Uploaded fields are available via c.req.parseBody().

基本示例

🌐 Basic example

ts
import { Hono } from 'hono'

const app = new Hono()

app.post('/upload', async (c) => {
  const body = await c.req.parseBody()
  const file = body['file']

  if (!(file instanceof File)) {
    return c.text('File is required', 400)
  }

  return c.json({
    name: file.name,
    size: file.size,
    type: file.type,
  })
})

限制上传大小

🌐 Limit upload size

ts
import { Hono } from 'hono'
import { bodyLimit } from 'hono/body-limit'

const app = new Hono()

app.post(
  '/upload',
  bodyLimit({
    maxSize: 5 * 1024 * 1024, // 5 MiB
    onError: (c) => {
      return c.text('File too large', 413)
    },
  }),
  async (c) => {
    const body = await c.req.parseBody()
    const file = body['file']

    if (!(file instanceof File)) {
      return c.text('File is required', 400)
    }

    return c.text(`Uploaded ${file.name}`)
  }
)

多个文件

🌐 Multiple files

ts
import { Hono } from 'hono'

const app = new Hono()

app.post('/upload', async (c) => {
  const body = await c.req.parseBody({ all: true })
  const value = body['file']

  const files = Array.isArray(value)
    ? value.filter((item): item is File => item instanceof File)
    : value instanceof File
      ? [value]
      : []

  if (files.length === 0) {
    return c.text('At least one file is required', 400)
  }

  return c.json({
    count: files.length,
    files: files.map((file) => ({
      name: file.name,
      size: file.size,
      type: file.type,
    })),
  })
})

在持久化之前进行验证

🌐 Validate before persisting

在将上传的文件写入磁盘或转发到其他服务之前,验证预期字段是否存在,并检查元数据,例如文件名、MIME类型和大小。

🌐 Before writing uploaded files to disk or forwarding them to another service, validate that the expected fields exist and check metadata such as filename, MIME type, and size.

另请参阅

🌐 See also

Hono 中文网 - 粤ICP备13048890号