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!