> ## 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.

# 将 ReadableStream 转换为 Uint8Array

要将 [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) 转换为 `Uint8Array`，先用 `Bun.readableStreamToArrayBuffer` 将其内容读取到 [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)，然后创建一个指向该缓冲区的 [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)。

```ts theme={null}
const stream = new ReadableStream();
const buf = await Bun.readableStreamToArrayBuffer(stream);
const uint8 = new Uint8Array(buf);
```

`Bun.readableStreamToBytes` 可直接转换为 `Uint8Array`。

```ts theme={null}
const stream = new ReadableStream();
const uint8 = await Bun.readableStreamToBytes(stream);
```

***

参见 [Bun 的其他 `ReadableStream` 转换函数](/runtime/utils#bun-readablestreamto)。
