首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

Extjs-关于文件的上载

2013-03-01 
Extjs--关于文件的下载上文写了关于文件的上传,在这边记录下文件的下载。从总结来看,本人对文件下载的处理

Extjs--关于文件的下载

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

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


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

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


热点排行