Formdata enables asynchronous transfer of binary files (i.e. asynchronous file uploads)

Source: Internet
Author: User

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)

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.