axios获取文件流并下载文件

首先给axios设置 responseType:'blob'

下载方式:一、使用a标签下载

axios.post(url,data,{responseType:'blob'}).then(res => {
    const blob = new Blob([res.data]);//处理文档流
    const fileName = '资产列表.xlsx';
    const elink = document.createElement('a');
    elink.download = fileName;
    elink.style.display = 'none';
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
})

下载方式:二、使用fileDownload插件下载

git地址:https://github.com/kennethjiang/js-file-download

 Axios.get(url, {
    responseType: 'blob',
  }).then(res => {
    fileDownload(res.data, '测试.xlsx');
  });
原文地址:https://www.cnblogs.com/lovecode3000/p/13900886.html