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) {}); },

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM