FormData is empty when using jQuery ajax()
Turned out, I'm using jQuery 1.8.1 which doesn't support FormData
. Library update solved the problem.
Here is How I would do it:
- Openning and Closing
Form
tag should wrap all the input files! e.preventDefault();
should be at the beginning of the function, better practice.
JS:
$("#generate_params").submit(function(e) {
e.preventDefault();
if( window.FormData !== undefined )
//make sure that we can use FormData ie>9, chrome > 7, opera > 12 safari >5, android > 3 gecko mobile > 2, opera mobile >12 <- wil support XHR too
{
var formData = new FormData(this);
// you can append aditional values:
// formData.append("be",logmebehave);
$.ajax({
url: 'filesend.php', //Path to the server script that process the data
type: 'POST',
data: formData,
xhr: function() { },
success: function(response){},
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
} else {
//Fallback
}
return false;
});
FormData wil support multi file upload.
Add to your Form tag the attribute:
enctype="multipart/form-data"
Should work great!
NOTE: You may find that the FILES array is empty on server side page - In this case you need to make sure your server configuration allows file uploads, size limit of file upload is enough, post time is enough etc....
The best way to begin is to make sure that file uploads is allowed and then testing with very small files to be sure everything in your code is OK.