XMLHttpRequest Level 2 Adds a new interface--formdata 。
It makes it easier to interact with Ajax today. Before the Ajax was uploaded, the data of the form needed to be serialized. The biggest advantage of using FormData compared to normal Ajax is that we can upload binary files asynchronously.
While learning to do, I also try to do a upload image function, if you may have to use form submission or other black magic, now use Formdata can be implemented.
However, there are compatibility issues with Formdata, please see:
Https://developer.mozilla.org/zh-CN/docs/Web/API/FormData#.E6.B5.8F.E8.A7.88.E5.99.A8.E5.85.BC.E5.AE.B9.E6.80.A7
The asynchronous upload binary file code is as follows:
<! DOCTYPE html>varSUBBTN = document.getElementById ("subbtn"); Subbtn.onclick=function(){ varFormElement = document.getElementById ("Testform"); varFormData =NewFormData (formelement); $.ajax ({URL:"1.php", type:"POST", Data:formdata, ProcessData:false,//tell jquery not to process the data sentContentType:false,//tell jquery not to set the Content-type request headerSuccessfunction(data) {$ ("Body"). append (data); } }); }; </script></body><?PHPif($_files["File"] ["error"] > 0) { Echo"Error:".$_files["File"] ["Error"]. "<br/>"; }Else { Echo"File name:".$_files["File"] ["Name"]. "<br/>"; Echo"Type:".$_files["File"] ["Type"]. "<br/>"; Echo"Size:". ($_files["File"] ["Size"]/1024). "Kb<br/>"; Echo"Storage location:".$_files["File"] ["Tmp_name"]; }?>
Formdata enables asynchronous transfer of binary files (i.e. asynchronous file uploads)