使用ajax方式下載文件


先下載jquery.fileDownload.js

 引入js;

js代碼:

var url = "${pageContext.request.contextPath}/exportExcel/exportMemberFile";
            //實現Ajax下載文件
            $.fileDownload(url, {
                httpMethod : 'POST',
                data : $("#form1").serialize(),
                prepareCallback : function(url) {debugger
                    $("#exportBtn").attr("disabled", true);
                    $("#exportBtn").html("正在導出");
                },
                successCallback : function(url) {debugger
                    $("#exportBtn").attr("disabled", false);
                    $("#exportBtn").html("導出");
                },
                failCallback : function(html, url) {
                    alert("導出失敗");
                }
            });

后台response要進行如下設置;

response.setContentType("application/x-download");
        response.setCharacterEncoding("utf-8");// 處理編碼問題
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xls");// 表頭編碼問題
        Cookie fileDownload=new Cookie("fileDownload", "true");
        fileDownload.setPath("/");
        response.addCookie(fileDownload);

然后就可以用了


免責聲明!

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



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