当前,如果用户希望下载大型zip文件,则下载是流的。
这是通过获取一个端点来完成的,然后使用Streamsaver.js将下载流传输到他们的浏览器,如下所示。
function download(id, fileName) {
const endpoint = `.../extract/downloads/zip_download/?id=${id}`;
return fetch(endpoint, requestOptions.get()).then(res => {
const downloadSize = res.headers.get("content-length");
const fileStream = createWriteStream(fileName, { size: downloadSize });
const writer = fileStream.getWriter();
if (res.body.pipeTo) {
writer.releaseLock();
return res.body.pipeTo(fileStream);
}
const reader = res.body.getReader();
const pump = () =>
reader
.read()
.then(({ value, done }) =>
done ? writer.close() : writer.write(value).then(pump)
);
return pump();
});
}这在Chrome中很好,但是我遇到了Firefox和Safari的问题。我明白的问题是:
TypeError: undefined is not a constructor (evaluating 'new streamSaver.WritableStream')还有什么其他方法可以解决这个问题呢?当然,一定有一种通用的方式来流我正在丢失的一个大的下载?
发布于 2022-02-07 23:28:27
我遇到了同样的问题,并将web-streams-polyfill包包含在我的项目中以修复它。目前,一些非铬浏览器似乎不支持WritableStream.
对于我自己来说,我只是在我的index.html中包含了这个脚本标记
<script src="https://unpkg.com/web-streams-polyfill/dist/polyfill.min.js"></script>
https://stackoverflow.com/questions/62325140
复制相似问题