UploadDialog使用完整代码

原文:http://blog.csdn.net/primary_wind/article/details/24592305


1.导入uploaddialog需要的js库

附上下载地址,里面有所用到的方法的介绍

http://www.max-bazhenov.com/dev/upload-dialog-2.0/

1.首先创建dialog对象

[javascript]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. var uploadDialog = Ext.create("Ext.ux.UploadDialog.Dialog", {  
[javascript]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. url:"uploadexl.do"//处理请求servlet  
  2. shadow : true,  
  3. post_var_name:"uploadFiles",   
  4. draggable:true,   
  5. resizable:true,   
  6. constraintoviewport:true,   
  7. modal:true,   
  8. reset_on_hide:false,   
  9. allow_close_on_upload:false,   
  10. upload_autostart:false,   
  11. base_params:{},   
  12. post_var_name:"file",   
  13. permitted_extensions:["xls"], //允许上传文件后缀  
  14. );  
2.编写各种状态响应的操作方法,并注册

[javascript]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. onUploadSuccess = function (dialog, filename, resp_data, record) {  
  2.         Ext.Msg.alert("提示","文件已经上传完毕!");  
  3.         store.load();  
  4.     };  
  5.       
  6. onUploadFailed = function (dialog, filename, resp_data, record) {  
  7.         Ext.Msg.alert(resp_data.data);  
  8.     };  
  9.       
  10. onUploadComplete = function (dialog) {  
  11.         Ext.Msg.alert("提示","文件已经上传完毕!");  
  12.         uploadDialog.hide();  
  13.         store.load();  
  14.     };  
  15.       
  16. uploadDialog.on("uploadsuccess", onUploadSuccess);  
  17. uploadDialog.on("uploadfailed", onUploadFailed);  
  18. uploadDialog.on("uploaderror", onUploadFailed);  
  19. uploadDialog.on("uploadcomplete", onUploadComplete);  


3.编写uploadservlet,需要用到另外的jar包进行上传文件的读取,可以搜索org.apache.commons.fileupload进行下载导入即可。

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package view;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7.   
  8. import javax.servlet.ServletConfig;  
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.http.HttpServlet;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13.   
  14. import org.apache.commons.fileupload.FileItem;  
  15. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
  16. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  17.   
  18. import data.UserManagerDAO;  
  19.   
  20. public class UploadServlet extends HttpServlet {  
  21.     private static final long serialVersionUID = 1L;  
  22.     public static String uploadPath = "";  
  23.   
  24.     public void init(ServletConfig config) throws ServletException {  
  25.         uploadPath = config.getServletContext().getRealPath("/") + "upload/";  
  26.         super.init(config);  
  27.     }  
  28.   
  29.     @SuppressWarnings("deprecation")  
  30.     protected void doPost(HttpServletRequest request,  
  31.             HttpServletResponse response) throws ServletException, IOException {  
  32.         request.setCharacterEncoding("utf-8");  
  33.         response.setContentType("text/html;charset=UTF-8");  
  34.         try {  
  35.             DiskFileItemFactory factory = new DiskFileItemFactory();  
  36.             factory.setSizeThreshold(4096);  
  37.             File path = new File(uploadPath);  
  38.             if (!path.exists()) {  
  39.                 path.mkdir();  
  40.             }  
  41.             factory.setRepository(path);  
  42.   
  43.             ServletFileUpload upload = new ServletFileUpload(factory);  
  44.             upload.setSizeMax(2147483647L);  
  45.             List<FileItem> fileItems = null;  
  46.             fileItems = upload.parseRequest(request);  
  47.             Iterator<FileItem> i = fileItems.iterator();  
  48.             while (i.hasNext()) {  
  49.                 FileItem fi = (FileItem) i.next();  
  50.                 String fileName = fi.getName();  
  51.                 int start = fileName.lastIndexOf("\\");  
  52.                 String paths = fileName.substring(start + 1, fileName.length());  
  53.                 System.out.println(fileName);  
  54.                 fi.write(new File(uploadPath + paths));  
  55.   
  56.             }  
  57.             File file = new File(uploadPath);  
  58.             uploadPath = uploadPath.replaceAll("/""\\\\");  
  59.             String test[];  
  60.             test = file.list();  
  61.             for (int b = 0; b < test.length; b++) {  
  62.                 String getfilename = uploadPath + test[b];  
  63.                 getfilename = getfilename.replaceAll("\\\\", "/");  
  64.                 if (getfilename.endsWith(".xls")) {  
  65.                     UserManagerDAO.saveExcel(getfilename);  
  66.                 }  
  67.             }  
  68.             response.getWriter().write("{success:true,message:'文件上传成功'}");  
  69.         } catch (Exception e) {  
  70.             System.out.println(e.getMessage() + " : " + e.getCause());  
  71.             response.getWriter().write("{success:false,message:'error...'}");  
  72.         }  
  73.     }  
  74. }  


4.完毕!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Configuration. Most configuration options are inherited from Ext.Window (see ExtJs docs). The added ones are: url - the url where to post uploaded files. base_params - additional post params (default to {}). permitted_extensions - array of file extensions which are permitted to upload (default to []). reset_on_hide - whether to reset upload queue on dialog hide or not (default true). allow_close_on_upload - whether to allow hide/close dialog during upload process (default false). upload_autostart - whether to start upload automaticaly when file added or not (default false). post_var_name - uploaded data POST variable name (defaults to 'file'). Events. filetest - fires before file is added into the queue, parameters: dialog - reference to dialog filename - file name If handler returns false then file will not be queued. fileadd - fires when file is added into the queue, parameters: dialog - reference to dialog filename - file name fileremove - fires when file is removed from the queue, parameters: dialog - reference to dialog filename - file name record - file record resetqueue - fires when upload queue is resetted, parameters: dialog - reference to dialog beforefileuploadstart - fires when file as about to start uploading: dialog - reference to dialog filename - uploaded file name record - file record If handler returns false then file upload will be canceled. fileuploadstart - fires when file has started uploading: dialog - reference to dialog filename - uploaded file name record - file record uploadsuccess - fires when file is successfuly uploaded, parameters: dialog - reference to dialog filename - uploaded file name data - js-object builded from json-data returned from upload handler response. record - file record uploaderror - fires when file upload error occured, parameters: dialog - reference to dialog filename - uploaded file name data - js-object builded from json-data returned from upload handler response. record - file record uploadfailed - fires when file upload failed, parameters: dialog - reference to dialog filename - failed file name record - file record uploadcanceled - fires when file upload canceled, parameters: dialog - reference to dialog filename - failed file name record - file record uploadstart - fires when upload process starts, parameters: dialog - reference to dialog uploadstop - fires when upload process stops, parameters: dialog - reference to dialog uploadcomplete - fires when upload process complete (no files to upload left), parameters: dialog - reference to dialog Public methods Better go see the source. I18n. The class is ready for i18n, override the Ext.ux.UploadDialog.Dialog.prototype.i18n object with your language strings, or just pass i18n object in config. Server side handler. The files in the queue are posted one at a time, the file field name is 'file'. The handler should return json encoded object with following properties:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值