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

然后就可以用了

posted @ 2019-04-28 11:14  十黎九夏  阅读(9843)  评论(0编辑  收藏  举报