Ajax上傳檔案,jqueryajax上傳檔案
先上
基於EazyUI開發
目前的單檔案上傳,還是偽進度條的,
至於做成真正動態進度條 需要ajax去後台擷取檔案大小跟總大小,
考慮目前專案檔都是10MB以下 所以就用了個偽進度條
上代碼
html
<form id="fileload" action="/ProcessDeployment/upload" target="fileupload" enctype="multipart/form-data" method="post"><table style="line-height: 30px; width:420px; margin:0px auto;"><tr><td style="text-align: right;">流程名稱:</td><td><input id="txtProcessName" name="txtProcessName" class="easyui-textbox"style="width:300px;" /></td></tr><tr><td style="text-align: right;">流程檔案:</td><td><input id="txtFile" name="txtFile" data-options="buttonText: '選擇zip檔案'"class="easyui-filebox" style="width:300px;" /></td></tr></table></form><iframe style="display:none" name="fileupload"></iframe><divstyle="text-align:center; line-height:50px; width:420px; margin:0px auto;"><button id="btnSave" class="easyui-linkbutton"data-options="iconCls:'icon-save'" style="width:70px;">儲存</button><button id="btnCancel" class="easyui-linkbutton"data-options="iconCls:'icon-cancel'" style="width:70px;">取消</button></div>
js
<p>try { debugger; $(function() { $("#btnCancel").bind("click", function() { $('#win').window('close'); });</p><p> $("#btnSave").bind("click", function() { debugger; var processName = $("#txtProcessName").textbox("getValue"); if (processName == "") { $.messager.alert('系統訊息', '請填寫流程名稱。', 'info'); return; } var filebox = $("#txtFile").filebox("getValue"); if (filebox == "") { $.messager.alert('系統訊息', '請選擇流程檔案。', 'info'); return; } else { if (filebox.toLowerCase().lastIndexOf(".zip") < 0) { $.messager.alert('系統訊息', '請選擇zip格式的檔案。', 'info'); return; } } $.messager.confirm('系統訊息', '您確定儲存嗎?', function(r) { if (r) { $.messager.progress({ title : '系統訊息', msg : '檔案上傳中...' }); setTimeout(function() { $("#fileload").submit(); var t = setInterval(function(){ $.post("/ProcessDeployment/cheackLoad",{flag:new Date().getTime()},function(data){ if(data.Result==true) { clearInterval(t); $.messager.progress('close'); $.messager.alert('系統訊息', '檔案上傳成功。', 'info'); $('#win').window('close'); $('#dg').datagrid('reload'); }
if(data.Result==false){ clearInterval(t); $.messager.alert('系統訊息', '檔案上傳失敗。', 'info'); } }); }, 1000); }, 1000); } }); }); });</p><p>} catch (e) {</p><p>}</p>
後台使用的是Jfinal
Controller
public void uploadindex() { setSessionAttr(GlobalKey.FileIsLoaded, ""); render("upload.jsp"); } public void cheackLoad() { Map<String, Object> map=new HashMap<String, Object>(); map.put("Result", getSessionAttr(GlobalKey.FileIsLoaded)); renderJson(JsonKit.toJson(map)); }
public void upload() { UploadFile file=null; InputStream in=null; ZipInputStream zipInputStream=null; try { //儲存檔案 file = getFile("txtFile"); String processName = getPara("txtProcessName"); in = new FileInputStream(file.getFile()); //InputStream in=this.getClass().getClassLoader().getResourceAsStream("diagrams/"+file.getFile().getName()); zipInputStream=new ZipInputStream(in); Deployment deployment= processEngines.getRepositoryService().createDeployment().name(processName).addZipInputStream(zipInputStream).deploy(); //---------------------------- System.out.println("部署ID:"+deployment.getId()); System.out.println("部署名稱:"+deployment.getName()); //---------------------------- zipInputStream.close(); in.close(); //代表部署成功,那麼就刪掉這個檔案 file.getFile().delete(); setSessionAttr(GlobalKey.FileIsLoaded, true); } catch (Exception e) { if(zipInputStream!=null) { try { zipInputStream.close(); } catch (IOException e1) { } } if(in!=null) { try { in.close(); } catch (IOException e1) { } } if(file!=null){ file.getFile().delete(); } setSessionAttr(GlobalKey.FileIsLoaded, false); } }
檔案儲存地址的配置
JfinalCfg.java類裡面
public void configConstant(Constants me) {// TODO Auto-generated method stub// 載入少量必要配置,隨後可用getProperty(...)擷取值loadPropertyFile("config.properties");// 設定設定檔的開發模式me.setDevMode(getPropertyToBoolean("devMode", false));// 設定頁面開發類型me.setViewType(ViewType.JSP);// 預設view的位置me.setBaseViewPath("/View");// 地址欄參數模式me.setUrlParaSeparator("-");//檔案上傳的存放地址,在webRoot下的WEB-INF/classes/diagrams檔案夾me.setUploadedFileSaveDirectory(PathKit.getWebRootPath()+"/WEB-INF/classes/diagrams");}
最後上傳成功之後的檔案
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。