> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 将 Uint8Array 转换为 ReadableStream

从 [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) 创建 [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) 的简单方法是使用 [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) 构造函数，并将整个数组作为单个数据块入队。对于较大的数据块，这种方法可能不理想，因为它实际上并没有“流式传输”数据。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
const arr = new Uint8Array(64);
const stream = new ReadableStream({
  start(controller) {
    controller.enqueue(arr);
    controller.close();
  },
});
```

***

若要以较小的数据块流式传输数据，首先从 `Uint8Array` 创建一个 `Blob` 实例。然后使用 [`Blob.stream()`](https://developer.mozilla.org/en-US/docs/Web/API/Blob/stream) 方法创建一个按指定大小分块流式传输数据的 `ReadableStream`。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
const arr = new Uint8Array(64);
const blob = new Blob([arr]);
const stream = blob.stream();
```

***

可以通过向 `.stream()` 方法传递一个数字来设置数据块的大小。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
const arr = new Uint8Array(64);
const blob = new Blob([arr]);

// 设置数据块大小为 1024 字节
const stream = blob.stream(1024);
```

***

查看 [文档 > API > 二进制数据](/runtime/binary-data#conversion) 获取使用 Bun 操作二进制数据的完整文档。
