JQuery Ajax Upload File Processing method Introduction (recommended) _jquery

Source: Internet
Author: User

AJAX is a technique for exchanging data with the server, and it can be used to update part of the page in the context of the entire page.

The following table lists all the JQuery AJAX methods:

jquery Ajax in the Web application development is very common, it mainly includes ajax,get,post,load,getscript and so on these several common no refresh operation method, next through this article to introduce jquery Ajax upload file processing mode.

Formdata objects

XMLHttpRequest Level 2 Adds a new interface formdata. Using the Formdata object, we can simulate a series of form controls using JavaScript with some key-value pairs, and we can also use the XMLHttpRequest send () method to commit the "form" asynchronously. The great advantage of using formdata is that we can upload a binary file asynchronously compared to ordinary Ajax.

Newer versions of all major browsers already support this object, such as Chrome 7+, Firefox 4+, IE 10+, Opera 12+, Safari 5+. It was written with the original JS XMLHttpRequest.

XMLHttpRequest Way

Xhr.open ("POST", Uri, true);
Xhr.onreadystatechange = function () {
if (xhr.readystate = 4 && xhr.status =) {
//Handle response.< C4/>alert (Xhr.responsetext); Handle response.
}
};
Fd.append (' myFile ', file);
Initiate a multipart/form-data upload
xhr.send (FD);

In fact, jquery Ajax can also be supported, the key is set: ProcessData and ContentType.

Ajax Way

var formData = new FormData ();
var name = $ ("input"). Val ();
Formdata.append ("File", $ ("#upload") [0].files[0]);
Formdata.append ("name", name);
$.ajax ({ 
url:url, 
type: ' POST ', 
data:formdata, 
//Tell jquery not to handle the sent data
Processdata:false, 
//Tell jquery not to set Content-type request header
Contenttype:false,
beforesend:function () {
Console.log (" In progress, please wait ");
success:function (responsestr) { 
if (responsestr.status===0) {
Console.log (Success) +RESPONSESTR);
} else{
Console.log ("failed");
}
, 
error:function (responsestr) { 
console.log ("error");
} 
});

Related Article

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.