前端解析二进制文件流并下载

  后台返回文件的二进制内容,前端转化下载

// 二进制流解析下载
const fileName = this.getHeadersFName(res. headers['content-disposition'].split(';'));
const blob = new Blob([res.data]);
const a = document .createElement('a') ;
const bUrl = window.URL.createObject URL(blob);
a.download = fileName;
a.href = bUrl;
document.body.appendChild(a);
a.click()
document .body.removechild(a);

// 获取文件名
getHeadersFName(conDspAttrs) {
  let fileName = '';
  conDspAttrs.forEach((item,index ) => {
    let temp = item.split('=');
    if(temp.length > 1 && temp[0] === 'filename') {
      fileName = temp
    }
  });
  return fileName
}

  

原文地址:https://www.cnblogs.com/harlem/p/14116353.html