无法将带有ajax的文件发送到php文件

我在接收文件数据时遇到问题。

这是我的HTML表单:

  Upload     

如您所见,它将我们发送到General.js文件

 $(document).ready(function (e){ $("#Form").on('submit',(function(e){ e.preventDefault(); var formData = new FormData(document.getElementById('userfile')); // AJAX Code To Submit Form. $.ajax({ type: "POST", url: "uploader.php", data: formData, cache: false, contentType: false, processData: false, success: function(callback){ alert(callback); } }); return false; }) ); }); 

和需要接收这个的php页面:

 $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; 

一切正常,但问题是当我将数据发送到php文件时,它没有收到它应该。 也许我需要使用一些额外的东西? 因为文件的名称和大小都是空的。

编辑:

 $(document).ready(function(){ $("#submit").click(function(){ var formData = new FormData($('form')[0]); // AJAX Code To Submit Form. $.ajax({ type: "POST", url: "uploader.php", xhr: function() { // Custom XMLHttpRequest var myXhr = $.ajaxSettings.xhr(); if(myXhr.upload){ // Check if upload property exists myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload } return myXhr; }, data: formData, cache: false, contentType: false, processData: false, success: function(callback){ alert(callback); } }); return false; }); }); function progressHandlingFunction(e){ if(e.lengthComputable){ $('progress').attr({value:e.loaded,max:e.total}); } } 

也试过这段代码,但它仍然没有向页面发送正确的信息。 我在uploader.php页面上看到一个错误,指出该文件的名称是空的,这意味着我可能没有收到它或者它根本没有发送。

试试这个……

  $(document).ready(function (e) { $("#Form").on('submit',(function(e) { e.preventDefault(); $.ajax({ url: "uploader.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 (ie 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 { console.log(data); } }); })); }); 

更改

  var formData = new FormData(document.getElementById('Form')); 

  var fd = new FormData(); fd.append("userfile", document.getElementById('userfile').value);