這幾天要做一個頁面的上傳和下載,從年前一直糾結到現在,一直以為是Extjs基礎下的上傳下載,其實與Extjs基本沒有什麼關係,只是一直思維被局限於Extjs介面的緣故。聽說Extjs4.0有對應的控制項,由於目前公司項目非4.0,所以目前暫未實現過,下面記錄一下自己目前總結的解決辦法。 上傳:[javascript] { xtype : 'textfield', fieldLabel : '上傳檔案', name : 'userfile', id : 'userfile', inputType : 'file', width : 300, height : 20 //emptyText: '請選擇上傳檔案...' 不知道為什麼 這兩個參數在IE8下都是無效的 // blankText : 'File can\'t not empty.' } 由一個Button觸發上傳:[javascript] { xtype : 'button', text : '上傳', name : 'doc_upbut', id : 'doc_upbut', handler : function() { if (form.form.isValid()) { var upFile = Ext.getCmp('userfile').getValue(); if(upFile == ''){ Ext.Msg.alert('錯誤','請選擇你要上傳的檔案'); return; } var upFileLastName = upFile.toString(); var LastNames = upFileLastName.split("\.") if(LastNames[LastNames.length-1] == ("exe")){ Ext.Msg.alert('錯誤','不支援該類型檔案!'); return; } var file_up = document.getElementById('userfile'); file_up.select(); var realpath = document.selection.createRange().text; realpath = encodeURIComponent(realpath);//IE8以上會出現一個虛擬路徑,在此要擷取真實路徑 Ext.MessageBox.show({ title : '請稍等', msg : '檔案正在上傳...', progressText : '', width : 300, progress : true, closable : false, animEl : 'loding' }); Ext.Ajax.request({ url : '../control/IndexDocument?act=updateLoad', params : { filePath : realpath, fatherPath :doc_majorCode }, method: 'POST', success: function (request ) { var resp=Ext.util.JSON.decode(request.responseText); if(resp.success == 'fail'){ Ext.Msg.alert('資訊','<center>上傳失敗!<p>'+ resp.Info+'</center>'); } else{ Ext.Msg.alert('提示', '上傳成功!'); doc_store.reload(); doc_grid.getView().refresh(); doc_store.commitChanges(); } }, failure: function ( result, request) { Ext.Msg.alert('錯誤','上傳時出現未知錯誤.'); } }); } }} handler處理:[java] public String updateLoad(HttpServletRequest request,HttpServletResponse response) { String jsonData = ""; String frompage = ""; try { String path = request.getSession().getServletContext().getRealPath(rootDir); String filePath = URLDecoder.decode(request.getParameter("filePath"),"utf-8"); String fatherPath = request.getParameter("fatherPath"); frompage= (request.getParameter("frompage") != null)? request.getParameter("frompage") : ""; jsonData = docUtil.updateLoad(filePath,fatherPath,path); } catch (UnsupportedEncodingException e) { e.printStackTrace(); jsonData = e.getMessage(); } if (!jsonData.equalsIgnoreCase("")) { jsonData = "{success:'fail',Info:'" + jsonData + "'}"; } else { jsonData = "{success:'success',Info:'上傳成功!'}"; } request.setAttribute("jsonData", jsonData); if(frompage.equals("")){ return "extDataPage"; }else if(frompage.equals("downloadFileListPage")){ return downloadFileList(request,response); }else{ return frompage; } } docUtil處理:[java] public String updateLoad(String filePath, String fatherPath, String path) { String ret=""; File upFileDir = new File(path + "\\" + fatherPath); if(!upFileDir.exists()){ upFileDir.mkdirs(); } String s[] = filePath.split("\\\\"); String fileName = s[s.length-1]; File inFile = new File(filePath); File outFile = new File(upFileDir.getPath() + "\\" + fileName); FileInputStream fis; FileOutputStream fos; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { fis = new FileInputStream(inFile); fos = new FileOutputStream(outFile); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); byte[] b = new byte[1024]; int len; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } bos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); ret="檔案上傳失敗!"; } catch (IOException e) { e.printStackTrace(); ret="檔案上傳失敗!"; }finally{ www.2cto.com try { bis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } return ret; }