Ajax file upload
With the latest browser from Safari 5/Firefox 4 we can use FormData class:
1 2 3 4 |
var data = new FormData(); jQuery.each(jQuery('#file')[0].files, function(i, file) { data.append('file-'+i, file); }); |
So now we have a object of FormData , which can be sent along with the XMLHttpRequest.
1 2 3 4 5 6 7 8 9 10 11 |
jQuery.ajax({ url: 'php/upload.php', data: data, cache: false, contentType: false, processData: false, type: 'POST', success: function(data){ alert(data); } }); |
We can set the contentType option to false,which force jQuery not to add a Content-Type header for you, otherwise, the boundary string will be missing from it. Also,[…]