使用ajax方式下载文件
引入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);
然后就可以用了