通過隱藏iframe實現無重新整理上傳檔案操作,iframe上傳檔案

來源:互聯網
上載者:User

通過隱藏iframe實現無重新整理上傳檔案操作,iframe上傳檔案

其實在ajax出現之前,web應用也可以是無重新整理的,那時大多通過IFrame來做到這一點。當然Ajax出現之後,人們一窩蜂地投奔Ajax 的陣營了,iFrame 就乏人問津了。但是用iFrame來實現無重新整理上傳檔案確實一個很好的選擇。

解決辦法是通過一個隱藏的iframe來處理上傳操作我採用的是ReactJS,amazeui,nodejs

1.html target指向iframe的name,就是把上傳後的操作交給iframe來處理.

<form id="supplyformFile" name="formFile" method="post" target="frameFile"encType="multipart/form-data"><div className="am-form-file"><button type="button" className="am-btn am-btn-default am-btn-sm"><i className="am-icon-cloud-upload"></i> 選擇要上傳的檔案</button><input type="file" id="fileUp" onChange={this.UploadSupplyer} name="fileUp" /></div> <div id="supplyfile_div"></div></form><iframe id="frameFile" name="frameFile" style={{display: 'none'}}></iframe><input type="hidden" id="supplyfile" />

2.JS處理當檔案選擇後提交form

UploadSupplyer:function(){var path = document.all.fileUp.value;if(!path){return false;}$('.loadinfo').html('<p>檔案上傳中...</p>').removeClass("none");$('#supplyformFile').submit();},

3.nodejs伺服器處理,因為處理頁面是nodejs伺服器域,iframe中存在跨域的問題,所以需要用到H5的postMessage方法來傳遞參數給iframe外面的表單頁面

var fname = req.files.fileUp.path.replace("publicfiles", "").replace("public/files/", "");res.writeHead(200, {'Content-type' : 'text/html'});res.write('<script>');res.write('window.parent.postMessage("'+fname+'","*");');res.end('</script>');

4.JS處理上傳結果

window.addEventListener('message',function(e){var fname=e.data;$('#supplyfile').val(fname);$(".loadinfo").addClass("none");$(".successinfo").html("<p>檔案上傳成功</p>").removeClass("none");setTimeout(function() { $(".successinfo").addClass("none");}, 2000);$("#supplyfile_div").html('<span class="am-icon-file-o"></span> <a target="_blank" href="'+hosts+'/files/'+fname+'">供應商確認單</a>');},false);

以上所述是小編給大家介紹的通過隱藏iframe實現無重新整理上傳檔案操作,希望對大家有所協助!

您可能感興趣的文章:
  • ajax(iframe)無重新整理提交表單、上傳檔案
  • php利用iframe實現無重新整理檔案上傳功能的代碼
  • php+iframe實現隱藏無重新整理上傳檔案
  • asp.net中MVC藉助Iframe實現無重新整理上傳檔案執行個體
  • 談談基於iframe、FormData、FileReader三種無重新整理上傳檔案的方法

聯繫我們

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