Somebody can answer please, once and for all, if is it possible to upload files using AJAX?
I read a few posts on the web that stating that file upload using AJAX is impossible!
If it is possible, can somebody please provide a working piece of code of AJAX request?
I already tried about 10 examples which I found on the web and no one is working.
Please do not refer me to plugins. I would like to understand how it works and implement it myself.
Thanks in advance!
Try this,
HTML
<input id="pic "type="file" name="file" onchange="javascript:this.form.submit();">
JS:
$("#pic").change(function() {
var file_data = $('#pic').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(dat){
alert('it works maybe');
}
});
});