> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/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={null}
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={null}
const arr = new Uint8Array(64);
const blob = new Blob([arr]);
const stream = blob.stream();
```

***

向 `.stream()` 传递一个数字来设置块大小。

```ts theme={null}
const arr = new Uint8Array(64);
const blob = new Blob([arr]);

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

***

参见 [二进制数据](/runtime/binary-data#conversion)。
