js無重新整理上傳檔案

來源:互聯網
上載者:User

標籤:data   速度   資訊   sub   pre   blog   傳統   ons   相容   

傳統的檔案上傳方式

<form action="" method="POST" enctype="multipart/form-data"><input type="file" name="file"/><input type="submit"/></form>

在執行上傳後,頁面會跳轉到action指定的地址。

一般使用者需要使用無頁面重新整理的上傳方式上傳檔案,此時可採取iframe的方式,將form表單提交後的返回內容,重新導向到iframe中。

修改後的代碼為

<form action="" method="POST" target="target" enctype="multipart/form-data"><input type="file" name="file"/><input type="submit"/></form><iframe name="target" id="target" frameborder="0" width="0" height="0"></iframe><script>    var iframe = document.getElementById("target");    var uploadCallback = function () {        console.log(iframe.contentDocument.body.innerHTML);    };    iframe.onload = uploadCallback;</script>

利用iframe的onload事件,檢測是否傳輸完畢,並擷取傳輸後的服務端傳回值。

樣本非常簡單,會有瀏覽器不相容的情況,但上傳外掛程式的基本思想如此。

 

此外還有swf方式的實現方式,可現實上傳速度以及進度等資訊。

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.