blob文件下载记录

export function downloadBlob(
  data,
  name,
  type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
) {
  if (name) {
    if (name.lastIndexOf(".") < 0) {
      name += ".xlsx";
    }
  } else {
    name += ".xlsx";
  }
  let blob = new Blob([data], { type });
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, name);
  } else {
    let objectUrl = URL.createObjectURL(blob);
    var a = document.createElement("a");
    a.download = name;
    a.href = objectUrl;
    a.click();
    document.body.appendChild(a);

    var evt = document.createEvent("MouseEvents");
    evt.initEvent("click", false, false);
    a.dispatchEvent(evt);
    document.body.removeChild(a);
  }
}

/**
 * 导出文件下载
 * @param  {[type]} res 后台返回response
 * @return {[type]}     [description]
 */
export function exportDownload(that,res){
  if (res.data.type == 'application/json') {
      // 将blob文件流转换成json
      const reader = new FileReader();
      reader.readAsText(res.data);
      reader.onload = function (event) {
        const message = JSON.parse(event.target.result).message;
        that.$message.error(message);
      }
      return false;
    }
    const blob = new Blob([res.data]);
    let str = res.headers['content-disposition'];
    let filename = decodeURI(str.substr(str.indexOf('=') + 1));
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        //ie使用的下载方式
        window.navigator.msSaveOrOpenBlob(blob, filename);
     } else {
        let elink = document.createElement("a");
        // 设置下载文件名
        elink.download = filename;
        elink.style.display = "none";
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink);
    }
}


// 下载xls
export function downLoadFile (data, name) {
let a = document.createElement('a')
a.download = `${name}.xls`;
a.style.display = 'none'
//获取请求返回的response对象中的blob 设置文件类型,这里以excel为例
let blob = new Blob([data], { type: 'application/[vnd.ms](http://vnd.ms/)-excel' })
//创建一个临时的url指向blob对象,创建url之后可以模拟对此文件对象的一系列操作,例如:预览、下载
a.href = URL.createObjectURL(blob)
document.body.appendChild(a);
a.click()
document.body.removeChild(a);
}

/**
 * 下载流文件文件工厂函数
 * @param {*} param0 
 * @returns 
 */
export function axiosDownloadFactory({ url, params }) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url,
      params,
      responseType: 'blob'
    }).then((response) => {
      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement('a');
      const fileName = response.headers['content-disposition'].match(/=(.*)$/)[1]; // 此方法为获取后台文件名,也可以当作参数传递进来
      link.href = url;
      link.setAttribute('download', decodeURI(fileName));
      document.body.appendChild(link);
      link.click();
      resolve();
    }).catch((error) => {
      reject(error)
    })
  })
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容