0

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!

2

1 Answer 1

3

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');
                }
     });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.