Using hidden iframe to achieve no refresh upload file operation _javascript Skill

Source: Internet
Author: User

In fact, before the advent of Ajax, Web applications can also be without refreshing, when most through the IFRAME to do this. Of course, after the advent of Ajax, people swarm to the Ajax camp, the IFrame is not a concern. However, it is a good choice to use an IFRAME to implement a no-flush upload file.

The solution is to handle the upload via a hidden iframe. I'm using Reactjs,amazeui,nodejs.

1.html target points to the name of the IFRAME, which is handing over the uploaded operation to the 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> Select File to upload
</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 processing when file selection is submitted form

Uploadsupplyer:function () {
var path = Document.all.fileUp.value;
if (!path) {return false;}
$ ('. Loadinfo '). html (' <p> file crosses ...</p> '). Removeclass ("none");
$ (' #supplyformFile '). Submit ();

3.nodejs server processing, because the processing page is a NODEJS server domain, there are cross-domain problems in the IFRAME, so you need to use the H5 PostMessage method to pass the parameter to the form page outside the IFRAME

var fname = req.files.fileUp.path.replace ("Publicfiles", ""). Replace ("public/files/", "");
Res.writehead ({' Content-type ': ' text/html '});
Res.write (' <script> ');
Res.write (' Window.parent.postMessage ("' +fname+ '", "*");
Res.end (' </script> ');

4.JS Processing Upload Results

Window.addeventlistener (' message ', function (e) {
var fname=e.data;
$ (' #supplyfile '). Val (fname);
$ (". Loadinfo"). AddClass ("none");
$ (". Successinfo"). HTML ("<p> file Upload succeeded </p>"). Removeclass ("none");
settimeout (function () {$ (". Successinfo"). AddClass ("none");
$ ("#supplyfile_div"). html (' <span class= am-icon-file-o ' ></span> <a target= ' _blank ' href= ' ' +hosts+ '/ files/' +fname+ ' "> Supplier Confirmation Sheet </a>");
},false);

The above is a small series to introduce you to the implementation of the hidden iframe without refreshing upload file operation, I hope to help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.