问题描述
- 新手求教!!关于使用struts 下载csv文件的问题 急
-
我配置好struts后,点击下载的时候页面没有任何的响应,这是为什么啊?
struts.xml<action name="xxDownload" class="com.xx.action.xxAction" method="download" > <result name="success" type="stream"> <param name="contentType" >application/csv</param > <param name="inputName" >stream</param> <param name="contentDisposition" > attachment;filename="${fileName}" </param> <param name="bufferSize" >4096</param> </result>
xxAction
private InputStream stream; String fileName; public String download(){ try { xx= service.search(condition); fileName = "xx_"+Math.random()+".csv"; File file = new File(fileName); OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); String str = ".........shenglve "; out.write(str.getBytes()); out.flush(); for (int i = 0; i < x.size(); i++) { str = xx.get(i).getStoreCD() + "," + xx.get(i).getProductCD() + "," + xx.get(i).getStartDate() + "," + xx.get(i).getEndDate() + "," + xx.get(i).getMassExposure() + ",aa "; out.write(str.getBytes()); out.flush(); } out.flush(); stream = new FileInputStream(file); } catch (Exception e) { logger.error(e.getMessage(),e); exFlag = "1"; } return "success"; }
js
var msg ="是否下载?"; if(window.confirm(msg)){ var url = "xxDownload"; var param = { "xx.a":"1" ,"xx.b":"2" ,"xx.c":"3" ,"xx.d":"" }; $.post(url,param); }
解决方案
解决方案二:
post不会引起跳转,也不会触发下载,需要用window.location=xxx跳转引发下载。
解决方案三:
下载的时候,别用这种方式,我用的from表单,提交的,但是如果你下载,包括下载报表之类的,下不了,这是异步传输的
时间: 2024-09-11 21:19:23