vue 导出流文件excel

第一种方法:这里需要安装 npm install js-file-download --save ,然后引用 var fileDownload = require('js-file-download'),使用详情见github;

Vue.axios.post(url_post,params_post,{responseType: 'arraybuffer'}).then((res) => {
  let fileName = res.headers['content-disposition'].match(/fushun(\S*)xls/)[0]; 
  fileDownload(res.data,fileName); 
}).catch((res) => { 
  // 错误信息 
})

第二种方法:需要设置响应类型,并用到Blob类型,了解Blob

Vue.axios.post(url_post,params_post,{responseType: 'arraybuffer'}).then((res) => {
    
    let blob = new Blob([res.data], {type: "application/vnd.ms-excel"}); 
    let objectUrl = URL.createObjectURL(blob); 
    window.location.href = objectUrl;  

}).catch((res) => {
    // 错误信息
})

猜你喜欢

转载自www.cnblogs.com/kdcg/p/9099455.html