jquery ajax send file code example
Example 1: ajax file upload jquery
$('#upload').on('click', function() {
var file_data = $('#sortpicture').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
alert(form_data);
$.ajax({
url: 'upload.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
alert(php_script_response);
}
});
});
Example 2: send parameters with file ajax jquery
$(function() {
$('button[type=submit]').click(function (event) {
event.preventDefault();
var form = this.form;
var data = new FormData(form);
var url = 'wherever.php';
$.ajax({
type: 'POST',
url: url,
data: data,
processData: false,
contentType: false
});
});
});