使用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