vue axios 请求二进制流excel文件,response乱码

https://img1.mukewang.com/5cb2a3600001b61508000126.jpg

问题:当axios发起post请求,后端返回的是二进制流excel文件,前台获取response时出现乱码问题,求解决方法

https://img3.mukewang.com/5cb2a3620001a0c907590260.jpg

response返回乱码数据。

 downloadModel(){

        // window.location.href = window.open(axios.defaults.baseURL +'/settlement/payableCheck/download/template');

        // let downLoadModel = window.open();


        // let fileDownload = require('js-file-download');

        this.$post('/settlement/payableCheck/download/template',{params:null},{responseType: 'arraybuffer'}).then(res => {

        // this.$get('/settlement/payableCheck/download/template').then(res => {

          console.log(res);

          // let fileName = res.headers['content-disposition'].match(/fushun(\S*)xls/)[0];

          // fileDownload(res,fileName);


          let blob = new Blob([res], {type: "application/vnd.ms-excel;charset=utf-8"});


          // let objectUrl = URL.createObjectURL(blob);

          // window.location.href = objectUrl;


          var link = document.createElement('a');

          link.href = window.URL.createObjectURL(blob);

          link.download = "对账模板";

          link.click();


        }).catch((e) => {

          this.$message.warning('下载失败');

          console.log(e);

        })

      },

https://img4.mukewang.com/5cb2a36e0001667908000552.jpg

强行text打开还是乱码

慕妹3242003
浏览 2822回答 5
5回答

慕容森

response乱码不是很正常吗?你强行用text打开一下你的excel看看是不是乱码?你看下载文件就行了,不用管response的文本输出。

www说

这个乱码应该是后端设置的编码问题

米琪卡哇伊

responseType: 'blob' blob 设置这个类型呢? {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"} ,还有 别用编辑打开excel啊
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript