1. 程式人生 > >vue2.x 下載後臺傳過來的流檔案(excel)後亂碼問題(轉載)

vue2.x 下載後臺傳過來的流檔案(excel)後亂碼問題(轉載)

找了這個問題找了好久  終於找到可行的辦法 轉載一下 原文連結點選開啟連結

1.介面返回的流和頭部:

2.下載流檔案的程式碼

方法一:是用了外掛 https://github.com/kennethjiang/js-file-download

方法二:是用了 blob

不管哪種方法,記得設定  responseType  !!!!!

附上程式碼:

複製程式碼
//      匯出訂單
      exportBill:function () {

        let url_post = Vue.prototype.api.apiList.EXPORT_BILL;

        let params_post = {
          orderStartDate: 
this.timepickerDateFormat(this.rangeTime[0]) || this.rangeTime[0] || '', orderEndDate: this.timepickerDateFormat(this.rangeTime[1]) || this.rangeTime[1] || '', prodCode: this.prodId, promoteFlag: this.promotionSiteId, policyStatusList: this.tableBillStateCheckedData, }; 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); //如果用方法一 ,這裡需要安裝 npm install js-file-download --save ,然後引用 var fileDownload = require('js-file-download'),使用詳情見github;
      // let blob = new Blob([res.data], {type: "application/vnd.ms-excel"}); 

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

      // window.location.href = objectUrl;

    }).
catch(function (res) {}); },
複製程式碼