標籤:blog io ar 使用 java sp 檔案 div 2014
ajax是無法提交檔案的,所以在上傳圖片並預覽的時候,我們常常使用Ifame的方法實現看似非同步效果。可是這樣總不是非常方便的,AjaxFilleUpload.js對上面的方法進行了一個封裝,使得我們不用去管理Iframe的一系列操作,也不用影響我們的頁面結構,實現非同步檔案提交。
html:
<input type="file" name="upload" hidden="hidden" id="file_upload" accept=".zip" />
js:
$.ajaxFileUpload({ url:'${pageContext.request.contextPath}/Manage/BR_restorePic.action', //須要連結到server地址 secureuri:false, fileElementId:'file_upload', //檔案選擇框的id屬性 dataType: 'text', //server返回的格式,能夠是json、xml success: function (data, status) //相當於java中try語句塊的使用方法 { $('#restoreDialog').html(data); //alert(data); }, error: function (data, status, e){ //相當於java中catch語句塊的使用方法 $('#restoreDialog').html("上傳失敗,請重試"); } });
這種方法還會出現一個問題,就是input僅僅能使用一次的問題,input第二次的onchange將不會被運行,這應該是與瀏覽器的有關,解決的方法就是替換這個input
像這樣:
$('#file_upload').replaceWith('<input type="file" name="upload" hidden="hidden" id="file_upload" accept=".zip" />');
使用AjaxFileUpload.js實現檔案非同步上?