③ 封装下载流

/*
* src > utils > download.js

* 封装的下载流的方法
* 参数 data 后台返回的数据流
* 参数 filename 下载的文件名
* 参数 mime 类型 转化的类型 详细可百度
* */
export function downloadStream(data, filename, mime) {
  const blob = new Blob([data], {type: mime || 'application/octet-stream'});
  if (typeof window.navigator.msSaveBlob !== 'undefined') {
    // IE 10以上
    window.navigator.msSaveBlob(blob, filename);
  } else {
    // 其他浏览器 h5属性 浏览器需要支持html5
    const blobUrl = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.style.display = 'none';
    a.href = blobUrl;
    a.setAttribute('download', filename);
    document.body.appendChild(a);
    a.click();
    setTimeout(() => {
      document.body.removeChild(a);
      window.URL.revokeObjectURL(blobUrl);
    }, 0);
  }
}
posted on 2021-10-22 09:53  pleaseAnswer  阅读(14)  评论(0编辑  收藏  举报