I need to download a large file with JavaScript using XMLHttpRequest or fetch without saving the file first in the RAM-Memory.
Normal link download doesn't work for me, because I need to send a Bearer Token in the header of the request.
我可以设法下载一个文件,但是这个“解决方案”,它首先将文件保存在 RAM 内存中,然后再出现保存对话框,这样如果文件大于可用的 RAM 内存,浏览器就会刹车。
这是我使用 fetch 的“解决方案”:
var myHeaders = new Headers();
myHeaders.append('Authorization', `Bearer ${token}`);
var myInit = { method: 'GET',
headers: myHeaders,
mode: 'cors',
cache: 'default' };
var a = document.createElement('a');
fetch(url,myInit)
.then((response)=> {
return response.blob(); …Run Code Online (Sandbox Code Playgroud)