簡體   English   中英

簡單的jQuery AJAX文件上傳

[英]Simple jQuery AJAX file upload

我在jQuery中完全是noob,並且迫切希望能夠讓它發揮作用。

這是html:

<form method="post" enctype="multipart/form-data">
    <input id="pic "type="file" name="file" onchange="javascript:this.form.submit();">
</form>

jQuery的:

$("#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: 'doupload.php',
                dataType: 'text',
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,
                type: 'post',
                success: function(dat){
                    alert('it works maybe');
                }
     });
});

所以我只想將文件發送到doupload.php並用( $_FILES['file']['tmp_name'] )捕獲它

但它沒有工作(ofc),我找不到任何有效的谷歌或堆棧......

我用這個lilbary: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>

<input id="pic "type="file" name="file" onchange="javascript:this.form.submit();">

你有“type =”文件“

將其更改為type =“file”

此外,如果你通過"$("#pic").change(function() {發送更改的ajax "$("#pic").change(function() {那么你應該沒有onchange="javascript:this.form.submit();"因為它將在ajax仍在發送時提交表單,從而導致可能的時間問題(例如ajax調用未完成)

據我所知,你根本不應該有一個提交事件,因為數據已經通過ajax調用提交了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM