admin管理员组文章数量:1122855
Extjs
上文写了关于文件的上传,在这边记录下文件的下载。从总结来看,本人对文件下载的处理其实完全是基于jsp,跟Extjs可以说是完全没有关系的,
要实现“另存为”的弹出,其实完全用Extjs也是可以的,但是本文用的是浏览器的解析,目前测试的是IE8。
jsp页面设置“下载”入口,然后从js跳转:
jsp
<a style="color:blue;font-weight:bold;text-decoration:underline" οnclick=' downloadfile("<%=file.getParentFile().getName() %>","<%= file.getName()%>"); '><span >下载文件</ span></a >
js
function downloadfile(fileDir,fileName){window.open("../assessment/downloadfile.jsp?loadDocDir="+fileDir+"&loadDocName="+encodeURI(encodeURI(fileName)),"_self");
}
或者直接js下设置“下载”入口实现跳转:
{id:'doc_download',header:"下载",width:100,dataIndex: 'doc_download',renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {var loadDocName = record.data["doc_name"];loadDocName = encodeURI(encodeURI(loadDocName));return '<a style="color:blue;font-weight:bold" href="../assessment/downloadfile.jsp?loadDocName='+loadDocName+'&loadDocDir='+doc_majorCode+'">下载</a>';}}
真正实现跳转的jsp页面:
String filePath = "";String loadDocDir = request.getParameter("loadDocDir")==null?"":(String)request.getParameter("loadDocDir");String loadDocName = request.getParameter("loadDocName")==null?"":(String)request.getParameter("loadDocName");loadDocName = java.net.URLDecoder.decode(loadDocName,"utf-8");if(!loadDocDir.equals("") && !loadDocName.equals("")){filePath = request.getSession().getServletContext().getRealPath("/assessment/document") + "\\" + loadDocDir + "\\" + loadDocName;}else{filePath = (String)request.getParameter("filePath");}File f = new File(filePath);if (f.exists()&&f.canRead()) {response.reset();response.setContentType ("application/octet-stream");response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(f.getName(),"utf-8"));response.setContentLength((int) f.length());BufferedOutputStream bos=null;BufferedInputStream bis=null;try{bis = new BufferedInputStream(new java.io.FileInputStream(filePath));bos = new BufferedOutputStream(response.getOutputStream());byte[] buff = new byte[1024];int bytesRead;while(-1 != (bytesRead = bis.read(buff, 0, buff.length))){bos.write(buff,0,bytesRead);}}catch(Exception ex){}finally{bis.close();bos.close();}response.flushBuffer();out.clear(); out = pageContext.pushBody(); bis=null;bos=null;}
当浏览器解析这个页面时,会自动弹出对应的 “另存为”,实现下载路径的选择
附上自己写的项目代码,
本文标签: Extjs
版权声明:本文标题:Extjs 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1688078869a176687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论