业务要求是这样的:从本地选择一个excel文件,服务器端解析该文件,并显示到页面上。思路是先将该excel文件上传,然后读取内容填充到页面上。 form的写法(去掉验证之类不必要的): var uploadForm =new Ext.form.FormPanel({ id:'_form', labelAlign:'right', frame:true, height:100, width:380, fileUpload:true, items:[ { id:'filepath2', xtype:'textfield', width:260, height:20, fieldLabel:'Excel文件', name:'attachment', inputType:'file' } ] , buttons:[ { text:'导入', handler:function(){ var p = Ext.getCmp('filepath2').getValue(); p = './Food/foodSchool_uploadFile.action?path=' + p; p = p.replace(/\\/g,'$'); if(uploadForm.getForm().isValid()){ uploadForm.getForm().url = p; uploadForm.getForm().submit({ waitMsg: '请稍等...', success:function(){ var conn = new Ext.data.Connection(); conn.request({// 发送异步请求 url : './Food/foodSchool_importReport.action', method : 'POST',//注意大写 params : { 'path' : p }, success : function(rsp, opt) { //导入成功后,利用AJAX执行另一个方法读取excel文件,并显示到页面上 ...... }, failure : function(rsp, opt) { Ext.Msg.alert('异常', '导入失败,请重新选择Excel文件!'); } }); win2.hide(); }, failure:function(){ Ext.Msg.alert('提示','导入失败!'); } }); } } } ] });
按钮的写法: var btn2 = new Ext.Button({ text:'导入', handler:function(){ win2 = win2 || new Ext.Window({ id:'uploadFile2', title:'选择导入的文件', modal:true, width:400, height:150, closeAction:'hide', layout:'fit', resizable:false, items:uploadForm }); win2.show(); } });
后台: public String uploadFile() { try { HttpServletRequest request = ServletActionContext.getRequest(); String path = request.getParameter("path"); path = new String(path.getBytes("ISO-8859-1"),"utf-8"); String projectPath = request.getSession().getServletContext().getRealPath("\\"); String fileName = path.substring(path.lastIndexOf("$") + 1); String newFilePath = projectPath + "warnning\\security\\food\\uploadTemp\\" + fileName; newFilePath = newFilePath.replaceAll("\\\\", "\\\\\\\\"); File uploadedFile = new File(newFilePath); FileUtils.copyFile(this.attachment, uploadedFile); } catch (Exception e) { e.printStackTrace(); } return null; } 至此结束 |
联系客服