如何使用 window.fetch 下载文件?
如果我想下载一个文件,我应该在下面的 then
块中做什么?
If I want to download a file, what should I do in the then
block below?
function downloadFile(token, fileId) {
let url = `https://www.googleapis.com/drive/v2/files/${fileId}?alt=media`;
return fetch(url, {
method: 'GET',
headers: {
'Authorization': token
}
}).then(...);
}
注意代码在客户端.
推荐答案
我暂时用download.js解决了这个问题 和 blob
.
let download = require('./download.min');
...
function downloadFile(token, fileId) {
let url = `https://www.googleapis.com/drive/v2/files/${fileId}?alt=media`;
return fetch(url, {
method: 'GET',
headers: {
'Authorization': token
}
}).then(function(resp) {
return resp.blob();
}).then(function(blob) {
download(blob);
});
}
它适用于小文件,但可能不适用于大文件.我想我应该更多地挖掘 Stream.
It's working for small files, but maybe not working for large files. I think I should dig Stream more.
相关文章