jquery file upload 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: jquery ajax upload image
$(function() {
$("#imagepicker").change(function(e) {
e.preventDefault();
var file_data = $(this).prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
form_data.append('action', 'ajax_image_upload');
$.ajax({
url: 'upload.php',
type: 'post',
contentType: false,
processData: false,
data: form_data,
success: function(response) {
console.log(response);
},
error: function(response) {
console.error(response.responseText);
}
});
});
});
Example 3: file upload in jquery
$(function(){
$('#uploadBTN').on('click', function(){
var fd = new FormData($("#fileinfo"));
$.ajax({
url: 'upload.php',
type: 'POST',
data: fd,
success:function(data){
$('#output').html(data);
},
cache: false,
contentType: false,
processData: false
});
});
});