当前位置: 动力学知识库 > 问答 > 编程问答 >

jQuery / AngularJS - Upload file with AJAX

问题描述:

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');
                }
     });
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: