后端返回二进制文件流,前端处理下载

处理逻辑:获取返回文件流,通过 Blob 对象构造文件后下载。

function download(data, filename, type="application/vnd.ms-excel") {
  let file = new Blob([data], {
      type: type
  });
  if (window.navigator.msSaveOrOpenBlob) {
      // IE10+
      window.navigator.msSaveOrOpenBlob(file, filename);
  } else {
      // Others
      let a = document.createElement("a");
      let url = URL.createObjectURL(file);
      a.href = url;
      a.download = filename;
      document.body.appendChild(a);
      a.click();
      setTimeout(() => {
          document.body.removeChild(a);
          window.URL.revokeObjectURL(url);
      }, 0);
  }
}

 

posted @ 2020-12-03 15:25  eightabs  阅读(1289)  评论(0编辑  收藏  举报