Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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!

share|improve this question
    
Take a look at the following link. developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/… –  DavidDomain Jun 25 at 11:48
    
Look at this github.com/blueimp/jQuery-File-Upload/wiki –  Michelem Jun 25 at 11:50

1 Answer 1

up vote 1 down vote accepted

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');
                }
     });
});
share|improve this answer
    
god bless you!!! –  daryqsyro Jun 25 at 12:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.