为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕妹3242003 2019-03-14 18:15:51
问题:当axios发起post请求,后端返回的是二进制流excel文件,前台获取response时出现乱码问题,求解决方法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);        })      },强行text打开还是乱码
查看完整描述

5 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

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


查看完整回答
反对 回复 2019-04-14
?
www说

TA贡献1775条经验 获得超8个赞

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


查看完整回答
反对 回复 2019-04-14
?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

responseType: 'blob' blob 设置这个类型呢? {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"} ,还有 别用编辑打开excel啊

查看完整回答
反对 回复 2019-04-14
  • 5 回答
  • 0 关注
  • 2821 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号