1. 程式人生 > >無重新整理實現檔案下載,同時帶有錯誤提示

無重新整理實現檔案下載,同時帶有錯誤提示

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