日期:2014-05-16  浏览次数:20397 次

Extjs--关于文件的下载

上文写了关于文件的上传,在这边记录下文件的下载。从总结来看,本人对文件下载的处理其实完全是基于jsp,跟Extjs可以说是完全没有关系的,

要实现“另存为”的弹出,其实完全用Extjs也是可以的,但是本文用的是浏览器的解析,目前测试的是IE8。


jsp页面设置“下载”入口,然后从js跳转:

jsp

<a style="color:blue;font-weight:bold;text-decoration:underline" onclick=' 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;
     }

当浏览器解析这个页面时,会自动弹出对应的 “另存为”,实现下载路径的选择


附上自己写的项目代码,http://download.csdn.net/detail/nisior/5095063