Skip to main content
要通过 HTTP 使用 Bun 上传文件,请使用 FormData API。首先,创建一个提供 HTML 表单的 HTTP 服务器。
index.ts

在另一个文件 index.html 中定义 HTML 表单。
index.html

运行服务器并访问 localhost:4000 查看表单。

表单会将表单数据通过 POST 请求发送到 /action 端点。在服务器中处理该请求。 首先,在传入的 Request 上调用 .formData(),以异步地将其内容解析为 FormData 实例。然后使用 .get() 提取 nameprofilePicture 字段;name 是一个 string,而 profilePicture 是一个 Blob 最后,使用 Bun.write()Blob 写入磁盘。
index.ts