使用流下载带有身份验证令牌的大文件

时间:2019-03-21 10:39:33

标签: download stream cross-browser polyfills

我正尝试使用Python 3.7.1 torch 1.0.1 (with Cuda 9.0) Windows 10 64-bit 下载文件,因为它的标题需要身份验证。问题是,当文件太大时,用户不得不等待很长时间才能从浏览器获得任何反馈,直到它说文件实际上已下载为止,因此我尝试进行流保存,以使“下载”出现浏览器中的“”菜单,同时进行下载。

  

enter image description here

虽然它可以在Chrome上运行,但我现在面临的问题是Firefox似乎不支持Stream API,因此我尝试使用polyfill,但似乎不起作用。

fetch

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题(how to consume the stream from Axios)。这是我解决的方法:

import { WritableStream } from 'web-streams-polyfill/ponyfill';
import streamSaver from 'streamsaver';

// If the WritableStream is not available (Firefox, Safari), take it from the ponyfill
if (!window.WritableStream) {
    streamSaver.WritableStream = WritableStream;
}

const fileStream = streamSaver.createWriteStream(fileName);

// The rest stays more or less the same, depends on the use case. For more detail in this section, refer to this example:
// https://jimmywarting.github.io/StreamSaver.js/examples/fetch.html

该想法是检查window.WritableStream在当前浏览器中是否可用。如果不是,请将WritableStream中的ponyfill直接分配给streamSaver.WritableStream属性。

在Google Chrome 78,Firefox 70,Safari 13上进行了测试; web-streams-polyfill 2.0.5StreamSaver.js 2.0.3