IE瀏覽器下載后台返回的Excel文件,報錯400


問題描述(見下圖):

 

 問題分析:

400是后端沒有接收到請求
原因是后端高版本的tomcat不會自動對字符串進行轉義
所以,前端把參數值進行轉義,即encodeURI(string)

問題處理前代碼(傳參數用一個字符串傳遞):

        exportExcel(){
            // 導出文件list的內容。
            let uu=$.getCookie('prefixUrl');
            let url=`${uu}/salesReport/exportPublicToExcel`;
            let cookie=$.getCookie('sessionId');
            // let cookie=encodeURI($.getCookie('sessionId'));
            let info='';  //這里是查詢條件。
            info=`"createYear":"${this.createYear}","createWeek":"${this.createWeek}"`;
            //type===3即客戶拜訪情況時,查看的是月
            let type = this.typeListVal
            if(type === 3){
                info=`"createYear":"${this.createYear}","createMonth":"${this.createMonth}"`;
            }
            // info = encodeURI(info)
            url=`${url}?type=${type}&sessionId=${cookie}&searchParams=${info}`;
            // window.location.href=url;
            // console.log(url)
            window.open(url);
        },

問題處理后代碼:

        exportExcel(){
            // 導出文件list的內容。
            let uu=$.getCookie('prefixUrl');
            let url=`${uu}/salesReport/exportPublicToExcel`;
            let cookie=$.getCookie('sessionId');
            // let cookie=encodeURI($.getCookie('sessionId'));
            let info='';  //這里是查詢條件。
            info=`"createYear":"${this.createYear}","createWeek":"${this.createWeek}"`;
            //type===3即客戶拜訪情況時,查看的是月
            let type = this.typeListVal
            if(type === 3){
                info=`"createYear":"${this.createYear}","createMonth":"${this.createMonth}"`;
            }
            info = encodeURI(info)
            url=`${url}?type=${type}&sessionId=${cookie}&searchParams=${info}`;
            // window.location.href=url;
            // console.log(url)
            window.open(url);
        },

  

將參數處理一下,就可以正常下載了

補充說明:也可以采用二進制流下載方式處理,可查看前面分享的文章

    還可以直接下面這樣,傳參方式改一下,所有參數都用&來拼接(需要后台配合)

 

        exportExcel(){
            // 導出文件list的內容。
            let vm = this;
            let goRealTime,returnRealTime;
            if(vm.goRealTime){
                goRealTime=new Date(vm.goRealTime).getTime();
            }else{
               goRealTime = ''; 
            }
            if(vm.returnRealTime){
                returnRealTime=new Date(vm.returnRealTime).getTime();
                returnRealTime = returnRealTime + 3600*24*1000;
            }else{
                returnRealTime = '';
            }
            let uu=$.getCookie('prefixUrl');
            let sessionId=$.getCookie('sessionId');
            // info=`"billType":"${this.billType}","startTime":"${stratT}","endTime":"${entT}","billCode":"${this.orderNumber}","departmentName":"${this.departmentName}","ownerName":"${this.applyUserName}"`;
            // url=`${url}?type=${type}&sessionId=${cookie}&searchParams=${info}`;
            let url=`${uu}/useVehicleApply/exportToExcel?sessionId=${sessionId}&orderNumber=${vm.orderNumber}&departmentName=${vm.departmentName}&applyUserName=${vm.applyUserName}&goRealTime=${goRealTime}&returnRealTime=${returnRealTime}&licensePlate=${vm.licensePlate}&applyStatus=${vm.applyStatus}&useCarType=0` ;
            // console.log(url);
            window.location.href=url;
        },

  


免責聲明!

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



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