前端 JS 经典:文件流下载

重点:调用接口时,一定要配置 responseType 的值为 blob,不然获取的文件流,不会转义成 blob 类型的文件。

1. 接口返回文件流

// BLOB (binary large object)----二进制大对象,是一个可以存储二进制文件的容器
// 下载接口:重点responseType: "blob"
// 返回体 res blob 文件流
function downloadFile(params) {
  return api({
    url: "/download/file",
    method: "get",
    params,
    responseType: "blob",
  });
}

2. 文件流下载:简单版

/**
 * 下载函数
 * @param {string} data - 后端获取的文件流
 * @param {string} name - 文件名
 */
function downloadFile(data, name) {
  const blob = new Blob([data]);
  const url = window.URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = name;
  document.body.appendChild(a);
  a.style.display = "none";
  a.click();
  document.body.removeChild(a);
}

3. 文件流下载:豪华版

/**
 * 全面优化下载函数
 */
function downloadFile(res) {
  // 判断是否接口调用是否正常返回文件流
  const r = new FileReader();
  r.readAsText(res.data);
  r.onload = () => {
    try {
      // 报错,未返回
      const resData = JSON.parse(r.result);
    } catch (err) {
      // 正常,开始转换文件流
      // 正常情况,浏览器不返回字段为 content-disposition 的请求头,
      // 需要后端特殊声明下,才拿得到,response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
      const name = res.headers["content-disposition"];
      const fileName = name.split("=")[1];
      // 解码
      filename = decodeURIComponent(fileName);
      // 兼容ie11
      if (window.navigator.msSaveOrOpenBlob) {
        try {
          const blobObject = new Blob([res.data]);
          window.navigator.msSaveOrOpenBlob(blobObject, fileName);
        } catch (e) {
          console.log(e);
        }
        return;
      }
      // a标签实现下载
      const url = window.URL.createObjectURL(new Blob([res.data]));
      const a = document.createElement("a");
      a.style.display = "none";
      a.href = url;
      a.download = filename;
      document.body.appendChild(a);
      a.click();
      resolve(fileName);
      document.body.removeChild(a);
    }
  };
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yqcoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值