# 错误：服务端操作无法直接从 FormData 接收 ArrayBuffer 或 Blob。请使用 File 对象或转换为 base64。

- **ID:** `nextjs/server-action-formdata-arraybuffer`
- **领域:** nextjs
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

服务端操作接收到的 FormData 包含 ArrayBuffer 或 Blob 条目，这些类型无法直接序列化用于服务端传输。Next.js 服务端操作仅支持来自 FormData 的 File、string 或 number 类型。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Next.js 14.x | active | — | — |
| Next.js 15.x | active | — | — |

## 解决方案

1. ```
   在客户端将 ArrayBuffer 转换为 base64 再附加到 FormData。示例：const base64 = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer))); formData.append('file', base64); 然后在服务端操作中解码：const buffer = Buffer.from(base64, 'base64');
   ```
2. ```
   在附加到 FormData 之前将 ArrayBuffer 包装在 File 对象中。示例：const file = new File([arrayBuffer], 'filename.bin'); formData.append('file', file); 服务端操作将接收到一个 File 对象。
   ```
3. ```
   不使用服务端操作，而是通过 fetch POST 请求将数据发送到直接接受二进制数据的 API 路由。
   ```

## 无效尝试

- **** — ArrayBuffer is binary data; JSON.parse will fail on it unless it is already a JSON string. This approach also does not address the serialization issue for the Server Action. (90% 失败率)
- **** — FormData serialization does not support ArrayBuffer; it will be converted to a string '[object ArrayBuffer]' or cause a TypeError during submission. (100% 失败率)
- **** — Next.js Server Actions handle FormData parsing internally before middleware runs. You cannot intercept the request body in middleware for Server Actions. (85% 失败率)
