If you are making a Ajax request to submit a form that include files then HTML5 FormData is the solution for that. You can also used jQuery form for upload files using Ajax request.
First, we need to add jQuery code to submit function which is called when our form will be submitted.
var form = $('#IdOfForm');
var formdata = false;
if (window.FormData) {
formdata = new FormData(form[0]);
}
$.ajax({
url : '/upload.php',
data : formdata,
cache : false,
contentType : false,
processData : false,
type : 'POST',
success : function(data, textStatus, jqXHR){
// Callback code
}
});
The FormData constructor encode all field to key/value pairs that exist in current form. The code for upload or copy the file will be written in upload.php file as per your need.
FormData is supported in all modern browsers. Only IE9 and below will cause issue.
Note: Don't forget to add enctype="multipart/form-data" to the form tag.
0 Comment(s)