Vue 采用blob下载后端返回的excel流文件乱码问题

  1. 没有文件服务器, 前后端采用文件流方式下载,后端返回二进制乱码时,前端使用blob对象进行处理
    2.采用的是axios请求方式
  2. this.$http.post("download", { fileName: file.filename })
    .then(function(response) {
    let blob = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
    let downloadElement = document.createElement('a');
    let href = window.URL.createObjectURL(blob); //创建下载的链接
    downloadElement.href = href;
    downloadElement.download = 'xxx.xlsx'; //下载后文件名
    document.body.appendChild(downloadElement);
    downloadElement.click(); //点击下载
    document.body.removeChild(downloadElement); //下载完成移除元素
    window.URL.revokeObjectURL(href); //释放掉blob对象
    })

4.分享链接 https://www.oschina.net/question/3910533_2283267

猜你喜欢

转载自www.cnblogs.com/wenjuan-blog/p/9346742.html