使用AjaxFileUpload.js實現檔案非同步上?

來源:互聯網
上載者:User

標籤: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實現檔案非同步上?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.