最简单的,页面中放的A标签,指向下载地址就可以下载了,(可以在href后加参数)
服务器端通过response写流就可以弹出下载框了,
有的可能是按钮,需要调方法后下载,
一、js添加form表单,提交下载
比用url请求的,看起来安全点
function exportExcel(){ var businessType = "w"; var catalogID = "1"; var f = document.createElement("form"); f.method = "post"; f.action = "/projectName/ExportExcelServlet"; f.appendChild(createInput("hidden", "businessType", businessType)); f.appendChild(createInput("hidden", "catalogID", catalogID)); document.body.appendChild(f); f.submit(); } //创建input元素 function createInput(type, name, value){ var i = document.createElement("input"); i.type = type; i.name = name; i.value = value; return i; }
二、js添加iframe,通过url下载
function exportExcel(){ var businessType = "w"; var catalogID = "1"; var url = "/projectName/ExportExcelServlet?businessType="+businessType+"&catalogID="+catalogID; exportByUrl(url); } /** * 通过iFrame实现类ajax文件下载 by zhao */ function exportByUrl(url) { var exportIframe = document.createElement('iframe'); exportIframe.src = url; exportIframe.style.display = "none"; document.body.appendChild(exportIframe); //hideWaitMsg(); }
相关:
poi导出excel,不使用模板的
http://happyqing.iteye.com/blog/2075985
HttpClient 3.1下载文件
http://happyqing.iteye.com/blog/2089621
HttpClient 4.1 下载文件