2016-12-02 15 views
0

這裏是我的html不確定的指數使用Ajax時上傳文件

<form id="uploadimage" name="form" action="" enctype="multipart/form-data"> 
    <input type="file" name="files" id="file" /> 
    <input type="submit" value="Upload"> 
</form> 

這裏是我的Javascript代碼

$(document).ready(function (e) { 
    $("#uploadimage").submit(function(e) { 
     e.preventDefault(); 
     $("#message").empty(); 
     $('#loading').show(); 
     $.ajax({ 
      url: "ajax_php_file.php", // Url to which the request is send 
      type: "POST",    // Type of request to be send, called as method 
      data:new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values) 
      contentType: false,  // The content type used when sending data to the server. 
      cache: false,    // To unable request pages to be cached 
      processData:false,  // To send DOMDocument or non processed data file it is set to false 
      success: function(data) // A function to be called if request succeeds 
      { 
       $('#loading').hide(); 
       $("#message").html(data); 
      } 
     }); 
    }); 
}); 

我想用ajax上傳文件。 JavaScript正在將數據正確地發送到php文件,但php代碼顯示錯誤未定義的索引文件。那麼,你能說出我在做什麼錯嗎?

這裏的PHP文件中的代碼

<?php 

       $filetype=$_FILES["files"]["type"]; 


?> 
+0

什麼'var_dump($ _ FILES)'show? – Barmar

+0

它顯示 數組(0){} – user2663856

+0

檢查Web控制檯的網絡選項卡,看看是否在您執行AJAX請求時發送'FormData'參數。 – Barmar

回答

0

我得到同樣的錯誤 - 遺漏的類型錯誤:無法讀取的未定義的屬性 '文件' 與jQuery Mobile的jquery.mobile-1.4.5.js和jquery.mobile-1.4 .js用於ajay文件上傳。

隨着jQuery Mobile的AJAX文件上傳的文件不支持,但它與jQuery庫工作2.1.1

請更換jquery.mobile-1.4.5.js到https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js

感謝