javascript實現iframe無重新整理上傳檔案

來源:互聯網
上載者:User

許多系統都會有上傳圖片這個功能,但是如果是用form表單提交的話,每次都會重新整理頁面,這樣體驗很不好;
今天分享一個利用iframe實現無重新整理上傳檔案的方法,直接上代碼:

html

<form class="form-horizontal text-sm" id="upload" name="upload[]" target="frm" action="/ERP/files/upFiles" method="post" enctype="multipart/form-data">
 <input type='file' id="file" name="files[]" accept=".csv" multiple="multiple" />
 <input type="hidden" name="MAX_FILE_SIZE" value="10485760" />  //限制檔案大小
</form>
<iframe id='frm' name='frm' style="display: none;"></iframe>

代碼

javascript

<script type="text/javascript">
 function callback(res){
   console.log(res);  //返回的對象
 }
 var frm = $("#frm");
 frm.load(function(){
   var wnd = this.contentWindow;
   var str = $(wnd.document.body).find("pre").html();
   str=JSON.parse(str);  //將json字串資料轉換成對象Object
   callback(str);
 });
</script>


原理:認真看的同學會發現,此處的iframe是隱藏的,此方法就是通過隱藏的iframe來提交資料,所以原來的頁面就不會被重新整理了;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.