Extjs 文件上傳


function fromExcel(){
    var dxjgdm_sel = Ext.get("dxjgdm").getValue();
    var dxjglx_sel = Ext.get("dxjglx").getValue();

   var form = new Ext.form.FormPanel({
     baseCls : 'x-plain',
     labelWidth : 150,
     fileUpload : true,
     defaultType : 'textfield',
     items : [{
        xtype : 'textfield',
        fieldLabel : '請選擇要導入的Excel文件',
        name : 'userfile',
        id : 'userfile',
        inputType : 'file',
        blankText : 'File can\'t not empty.',
        anchor : '100%' // anchor width by percentage
       }]
    });

    var win = new Ext.Window({
         title : 'Excel導入',
         width : 400,
         height : 150,
         minWidth : 300,
         minHeight : 100,
         layout : 'fit',
         plain : true,
         bodyStyle : 'padding:5px;',
         buttonAlign : 'center',
         items : form,
         buttons : [{
                 text : '導入',
                 handler : function() {
                   if (form.form.isValid()) {
                        if(Ext.getCmp('userfile').getValue() == ''){
                             Ext.Msg.alert('錯誤','請選擇你的文件');
                             return;
                          }
                        Ext.MessageBox.show({
                               title : '請等待',
                               msg : 'Excel正在導入...',
                               progressText : '',
                               width : 300,
                               progress : true,
                               closable : false,
                               animEl : 'loding'
                          });
                        form.getForm().submit({
                             url : 'ywcs_dxjgxx.app?dateTime=' + new Date().getTime()+ '&subTranCode=list&dxjgdm_sel='+dxjgdm_sel+'&fromexcel_sel=true&dxjglx_sel='+dxjglx_sel,
                             method : 'POST',
                             success : function(form, action) {
                                  Ext.Msg.alert("系統提示", "文件上傳成功!");
                                  win.close();
                             },
                             failure : function() {
                                  Ext.Msg.alert("系統提示", "文件上傳失敗!(上傳文件類型不對或文件過大)");
                             }
                        })
                       }else{
                           Ext.Msg.alert("系統提示","請選擇文件后再上傳!");
                       }
                  }
             }, {
                  text : '關閉',
                  handler : function() {
                       win.close();
                  }
             }]
        });
        win.show();
 
    
    
}
private void fromExcel() throws IOException, HsException {
        // TODO Auto-generated method stub
        
        FileItemFactory factory = new DiskFileItemFactory(); 
           ServletFileUpload fileload = new ServletFileUpload(factory); 
         
           fileload.setSizeMax(4194304); 
           boolean is_excel = false;
           
           try { 
               System.out.println(request);
               List<FileItem> fileItems = (List<FileItem>) fileload.parseRequest(request); 
               Iterator<FileItem> iter = fileItems.iterator(); // 依次處理每個上傳的文件
               while (iter.hasNext()) { 
                   FileItem item = (FileItem) iter.next();// 忽略其他不是文件域的所有表單信息 
                   if (!item.isFormField()) { 
                       String name = item.getName();//獲取上傳文件名,包括路徑 
                       System.out.println(name); 
                       String file_type = name.split("\\.")[1];
                       if(file_type.equals("xls") || file_type.equals("xlsx")){
                           is_excel = true;
                       }else{
                           is_excel = false;
                           System.out.println("上傳文件類型不對");
                           AjaxTools.exAjax("{success:false}", response);
                           return;
                       }
                       
                       if (name != null && is_excel) { 
                           File filetoserver = new File("D:\\", name); 
         
                           item.write(filetoserver); 
         
                           System.out.println("文件:" + name + "上傳成功,大小是" + item.getSize()); 
                           AjaxTools.exAjax("{success:true}", response);
                           return;
                       } 
                   } 
               }
           } catch (Exception e) { 
           } 
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM