2012-05-08 33 views
1

我想用jQuery的$.ajax函數上傳文件,但沒有得到任何輸出。 有人請幫我解決這個問題。 我不知道這個腳本是否正確。 我的腳本是:

$.ajax({ 
    url:'newsup.php', 
    data: "", 
    type: 'POST', 
    contentType:'multipart/form-data', 
    dataType: 'json', 
    catche: 'false', 

    success:function(data6) 
    { 
    $("#disp").removeClass().addClass((data6.error=== false)? 'success':'error').html(data6.msg).fadeIn('fast'); 
    //dele(); 
    if($("#disp").hasClass('success')) 
    { 
     alert("success"); 
     setTimeout("$('#disp').fadeOut('slow')",3000);    
    } 
    }, 

    error:function(XMLHttpRequest,textStatus,errorThrown) 
    { 
    $("#disp").removeClass().addClass('error').html("There was an <strong>"+errorThrown+"</strong> error due to <strong>"+textStatus+" condition").fadeIn('fast'); 
    }    

}); 

此外,我需要幫助使用jQuery從文件上傳現場獲取數據。

+0

查看plupload(http://www.plupload.com/)以獲取AJAX式表單上傳的示例。如果不使用嵌入式iframe來提交不可見的表單,則通過普通帖子在單個頁面中提交帶有文件的表單非常困難。 Plupload也很好地與jQuery集成(至少那是我的經驗)。 –

+0

你有做過這方面的任何谷歌?我希望有很多有用的結果。其中一個http://www.phpletter.com/Demo/AjaxFileUpload-Demo/ –

+0

如果你可以使用一個,https:// github,那麼有一個流行的和經過測試的jquery插件。com/blueimp/jQuery-File-Upload –

回答

2

AJAX不支持文件上傳。有像ajaxfileupload這樣的插件,它基本上創建了一個隱藏窗體並動態上傳你的文件。

看看here和閱讀奧利奇的回答

+0

我很感謝你回答我的問題,並感謝你的回答。 – JOE

6

請使用插件此。
在我看來這個插件是更好的解決方案。你不需要記住所有選項等。只需將你的'ajax'替換爲'ajaxForm'。

請仔細閱讀例子,下面
http://jquery.malsup.com/form/#ajaxForm

+1

我喜歡這個插件。我在我開發的每個網站中都使用它。當這個插件做得不錯時,手動AJAX處理表單毫無意義。 – xbonez

+0

我試過malsups $ ajaxForm插件,但我無法將存儲在變量中的數據傳遞給'$ ajaxForm()'函數。它只能通過直接從'$('#myForm1')。ajaxForm(options);''等形式獲取數據。我怎樣才能克服這個問題。 – JOE

+0

@JOSEPHPAUL你能提供更多的信息嗎?(你的代碼) – Oyeme

0

使用時只需在表格提交事件來發送文件,防止默認表單操作

$('#form').submit(function(e) { return false; });

,並獲取服務器上的文件側通過

$_FILES['inputName']; 
2

這就是我所做的。使用FormData對象。

注意:for語句的奇數語法只是將「f」設置爲數組[i]實例。

 $("#submit").click(function() { 
      var formData = new FormData(); 
      for (var i = 0, f; f = fileArray[i]; i++) { 
       formData.append("opmlFile", f); 
      } 
      $.ajax({ 
       url: "/Documents/SaveFiles/" + @Model, 
       type: "POST", 
       data: formData, 
       cache: false, 
       contentType: false, 
       processData: false 
      }) 
      .error(function (xhr, status, error) { 
       $.notify(error, true); 
      }) 
      .success(function (data, status, xhr) { 
       $.notify("Success"); 
      }); 
     }); 

不幸的是,我不記得哪一篇文章是我從中得到的,但它是Stack Overflow上的其他人。

1

我使用這個和它的正常工作:

$('#btnUploadFile').on('click', function() { 
       var data = new FormData(); 
       var files = $("#fileUpload").get(0).files; 

       // Add the uploaded file content to the form data collection 
       if (files.length > 0) { 
        data.append("upload", files[0]); 
       } 

       // Make Ajax request with the contentType = false, and procesDate = false 
       var ajaxRequest = $.ajax({ 
        type: "POST", 
        url: "/api/documents", 
        contentType: false, 
        processData: false, 
        data: data, 

        error: function (xhr, status, error) { 
         console.log(xhr); 
         console.log(status); 
         console.log(error); 
         console.log(data); 
        } 
       }); 

       ajaxRequest.done(function (xhr, textStatus) { 
        $("#response").attr('class', "alert alert-success"); 
        $("#response").html("File uploaded successfully"); 
       }); 


      }); 
+0

嗨,你如何上傳圖像和數據在同一時間..? – gayan1991

+1

@ gayan1991你可以建立'multipart/form-data'類型的請求/表單。見https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form -data-意思也可能有幫助 –