$.ajax异步请求无法下载文件到浏览器本地的问题

 

  //open这种方式有个弊端,就是转的参数值不能太大,所以只能用jquery创建表单form在提交(ajax异步请求不会触发浏览器下载文件的功能)
           var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
              if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1) {//判断是否IE浏览器
                       window.location.href(url);
                   }else{
                       window.open(url,"xxx");
                   }  
方法1,window.open

方法2 js前端

 //导出
               $("#exportData").click(function() {  
                   var myqx =document.getElementById("qxId").contentWindow.document.getElementById("myqx");
                   var myqxD = $(myqx).val();
                   var id = $("input[name='myHidId']").val();
                   var url = __ctx+'/hyddrbnew/hyddrbnew/hyddrb/exportExcelDdrb.ht';
                   
                   var form = $("<form>");
                   form.attr("style","display:none");
                   form.attr("target","_blank");
                   form.attr("method","post");
                   form.attr("action",url);
                   var input1 = $("<input>");
                   input1.attr("type","hidden");
                   input1.attr("name","id");
                   input1.attr("value",id);
                   
                   var input2 = $("<input>");
                   input2.attr("type","hidden");
                   input2.attr("name","myqxD");
                   input2.attr("value",myqxD);
                   
                   $("body").append(form);
                   form.append(input1);
                   form.append(input2);
                   form.submit();
                   form.remove();
  
               }); 
方法2 js前端

方法2(java后端)

@RequestMapping({ "exportExcelDdrb" })
    @Action(description = "导出excel")
    public void exportExcelDdrb(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = RequestUtil.getString(request, "id");
        String imageMsg = RequestUtil.getString(request, "myqxD");
            //取得实体类数据
        Hyddrb yjbb = new Hyddrb();
        yjbb = (Hyddrb) hyddrbService.getById(Long.valueOf(Long.parseLong(id)));
        
        String dirPath = FileUtil.getRootPath() + File.separator + "commons" + File.separator + "template" + File.separator + "hydlbbTemplate" + File.separator;
        String fileName = "expHyTabDdrb.xls";
//        String fileName = "expHyTabDdrb.xlsx";
        FileInputStream inStream = new FileInputStream(new File(dirPath + fileName));
        
        //xls2003版
        HSSFWorkbook wb = new HSSFWorkbook(inStream);
        this.drawImage(wb, imageMsg);
        this.initSheetCell(wb, yjbb);
        //xlsx2007版
//        XSSFWorkbook wbxlsx = new XSSFWorkbook(inStream);
//        this.drawImage_xlsx(wbxlsx, imageMsg);
//        this.initSheetCell_xlsx(wbxlsx, yjbb);
        
        fileName = "调度日报_" + new SimpleDateFormat("yyyy-MM-dd").format(yjbb.getRq()) ;
        //其实两套代码都可以,最终结果以下面给定的后缀作为生成excel的后缀,即03版还是07版
        fileName +=  ".xls";
//        fileName +=  ".xlsx";

        response.setContentType("application/excel");
        response.addHeader("Content-Disposition", "attachment;filename=" + new String((fileName).getBytes("gb2312"), "ISO8859-1"));
        OutputStream out = response.getOutputStream();
        wb.write(out);
//        wbxlsx.write(out);
        out.flush();
        out.close();

    }
方法2后端
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱跳舞的程序员.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值