作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试下载一个 10GB 的文件,但只有 4GB 保存到磁盘,而且内存增长了很多。
const res = await fetch('https://speed.hetzner.de/10GB.bin');
const file = await Deno.open('./10gb.bin', { create: true, write: true })
const ab = new Uint8Array(await res.arrayBuffer())
await Deno.writeAll(file, ab)
最佳答案
您正在缓冲响应,这就是内存增长的原因。
您可以遍历 res.body
因为它目前是 ReadableStream
实现Symbol.asyncIterator
并使用 Deno.writeAll
在每个 block 上。
for await(const chunk of res.body) {
await Deno.writeAll(file, chunk);
}
file.close();
fromStreamReader
来自
std/io
(
>= std@v0.60.0
) 转换为
res.body
到
Reader
可用于
Deno.copy
import { fromStreamReader } from "https://deno.land/std@v0.60.0/io/streams.ts";
const res = await fetch('https://speed.hetzner.de/10GB.bin');
const file = await Deno.open('./10gb.bin', { create: true, write: true })
const reader = fromStreamReader(res.body!.getReader());
await Deno.copy(reader, file);
file.close();
ArrayBuffer
/
UInt8Array
限制,因为 4GB 大约是 2³² 字节,这是
TypedArray
的限制,
at least in most runtimes .
关于javascript - 如何在 Deno 中下载大文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61945050/
我是一名优秀的程序员,十分优秀!