vue axios解决文件流下载乱码

前端请求头 {responseType :‘blob’} 一定要加,是单独一个对象,不能放在请求参数里面

new Blob([res],{type : ‘application/vnd.ms-excel;charset=utf-8’ 一定要设置类型,和后端response.setContentType(“application/vnd.ms-excel;charset=utf-8”)一样

 exportTableAPI: function (params){
      return axios.post('/module/device/export', params,{responseType :'blob'}, {appTitle: "导出表格"}).then(res => res.data);
    }

    //导出数据表
    exportBookingTable(){
      if(!this.checkTableList.length){
        this.$message.warning('请先勾选需要导出表格的行');
        return false;
      }
      let arrId = [];
      for(let item of this.checkTableList){
        arrId.push(item.deviceId);
      }
      api.exportTableAPI({'params':arrId}).then(res=>{
        const url = window.URL.createObjectURL(new Blob([res],{type : 'application/vnd.ms-excel;charset=utf-8'})); 
        const link = document.createElement('a'); 
        link.href = url;
        link.setAttribute('download', '台账列表.xls');
         document.body.appendChild(link); link.click(); 
         document.body.removeChild(link)
         console.log(url);
      })
    },

你可能感兴趣的:(vue.js,javascript,ecmascript)