javascript - 无法使用ajax将文件发送到php文件

标签 javascript php jquery ajax post

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

这是我的 HTML 表单:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Upload</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript" src="General.js"></script>
</head>

<body>
<form method="post" enctype="multipart/form-data" id="Form" action="upload.php">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="submit" type="submit" class="box" id="submit" value=" Upload "></td>
</tr>
</table>
</form>

如您所见,它将我们发送到文件 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 (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
            {
                 console.log(data);
            }
        });
    }));
});

关于javascript - 无法使用ajax将文件发送到php文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28661757/

相关文章:

php - PHP 和 MySQL 中的嵌套注释

php - Apache 2 mod_rewrite 和 PHP。从 htaccess 修改 $_SERVER ['REQUEST_URI' ] 值?

javascript - 错误 : [$rootScope:infdig] http://errors. angularjs.org/1.4.1/$rootScope

javascript - 链接 HTML 中的 javascript 文件导致 slider 消失

javascript - 使用 JSON 定义的函数时是否可以使用相对语法访问 JSON 属性?

javascript - 使用 UFT 自动化 AngularJS UI-Grid

javascript - 钛模块 imagefactory 将无法工作

javascript - CouchDB:获取唯一值

php - MySQL 查询中表名周围反引号的重要性

javascript - Ajax JQuery 中的表单提交