无刷新实现文件下载,同时带有错误提示

来源:互联网 发布:和彩云管家 网络异常 编辑:程序博客网 时间:2024/06/05 00:23

JS方法

//导出数据        function exportData(){            var url = "<%=request.getContextPath()%>/web/fms/receivedPaymentExportData";            var params = {};       //入参             $.ajax({                 type: "POST",                 url: url,                 data: params,                  success: function(response, status, request) {                         var disp = request.getResponseHeader('Content-Disposition');                         if (disp && disp.search('attachment') != -1) {  //判断是否为文件                             var form = $('<form method="POST" action="' + url + '">');                             $.each(params, function(k, v) {                                 form.append($('<input type="hidden" name="' + k +                                         '" value="' + v + '">'));                             });                             $('body').append(form);                             form.submit(); //自动提交                         }else if(!response.success){                             $.messager.alert("提示",response.message,"error");                         }                  },             });        }

后台Controller方法

@RequestMapping(value="/receivedPaymentExportData")    public void receivedPaymentExportData(@RequestParam(value="billtype",required=false)String billtype,@RequestParam(value="datebegin",required=false)String datebegin,@RequestParam(value="dateend",required=false)String dateend,HttpServletResponse resp) throws IOException{        Map<String, Object> mapParam = new HashMap<String, Object>();        ReturnMessage message = new ReturnMessage();        Gson gson  = new Gson();        try {            if(billtype != null && !"".equals(billtype)){                mapParam.put("billtype",billtype);            }            if(datebegin !=null && !"".equals(datebegin)){                mapParam.put("datebegin", datebegin);            }            if(dateend !=null && !"".equals(dateend)){                mapParam.put("dateend", dateend);            }            mapParam.put("pageIndex",1);            mapParam.put("pageSize", 10);            //post请求路径            StringBuffer url = new StringBuffer(ConstantUtil.NET_URL + "api/FinanceBill/getBillReceive");            String json = HttpPostUrl.sendPostWithpar(url.toString(),mapParam);            System.out.println(json);            JSONObject jsonObject = JSON.parseObject(json);    //          JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();            if ("true".equals(String.valueOf(jsonObject.get("Successed")))) {                Map<String,String> headMap = new HashMap<String,String>();                headMap.put("billid", "账单ID");                headMap.put("billcode", "订单编码");                headMap.put("billdate", "订单日期");                headMap.put("amount", "订单金额");                headMap.put("paytype", "支付类型");                headMap.put("status", "支付状态");                JSONArray ja = ((JSONObject)jsonObject.get("Data")).getJSONArray("Data");                ExcelUtil.downloadExcelFile("收入单数据", headMap, ja, resp);            }else{                message.setSuccess(false);                message.setMessage("数据导出异常!");                String str = gson.toJson(message);                resp.setContentType("applcation/json;charset=utf-8");                resp.getWriter().print(str);            }        }catch(Exception e){            message.setSuccess(false);            message.setMessage("导出数据请求异常!");            String str = gson.toJson(message);            resp.setContentType("applcation/json;charset=utf-8");            resp.getWriter().print(str);        }    }