2017-06-14 54 views
0

Ajax調用張貼的圖片上傳圖片:問題,使用AJAX PHP

function FileUploadCheck() { 
var formData = new FormData(); 
formData.append('LogoImageUploader', jQuery('#LogoImageUploader')[0].files[0]); 

    jQuery.ajax({ 
      type: "POST",  
      url: "FileUploadChecker.php", 
      contentType: "multipart/form-data",   
      data: formData, 
      processData: false, 
      contentType: false, 
      success : function(result){ 
       alert(result); 

      } 
     }); 
} 

我認爲圖像是越來越在PHP腳本結束成功 request payload for the post request

發佈:

<?php 

echo var_dump($_FILES); 

?> 

以上所有的代碼適用於我,當我使用jquery 1.7.2,但我不能繼續使用jquery作爲其他JS庫存在應用程序與jQuery 1.7.2不兼容。我需要知道,如果我只能導入任何獨立的JS(也許它提供了Ajax功能),使其工作類似於jQuery 1.7.2。

+0

你應該試試這個。 https://stackoverflow.com/a/18324384/4514250 –

+0

檢查此鏈接 https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload –

+0

如果我是正確的你缺少'enctype =「multipart/form-data「'在你的表單中... –

回答

0

我發現一個答案:

function FileUploadCheck() { 
var formData = new FormData(); 
formData.append('LogoImageUploader', jQuery('#LogoImageUploader')[0].files[0]); 

    var http = new XMLHttpRequest(); 
    var url = "FileUploadChecker.php"; 

    http.open("POST", url, true); 

    //when i uncommentated this line, the upload failed, as i tried to manually set the header to the post, but i let XHR to set the header automatically this works. 
    //http.setRequestHeader("Content-type", "multipart/form-data"); 



    http.onreadystatechange = function() {//Call a function when the state changes. 
     if(http.readyState == 4 && http.status == 200) { 
      alert(http.responseText); 
     } 
    } 
    http.send(formData); 
}