如何使用 window.fetch 下载文件? [英] How can I download a file using window.fetch?

查看:24
本文介绍了如何使用 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.

这篇关于如何使用 window.fetch 下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆