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

  

posted on 2019-04-09 15:00  Mike17  阅读(1177)  评论(0编辑  收藏  举报

导航

搜狗