这是我在这里的第一个问题(通常我喜欢找到自己解决问题的方法),但我在文件上传代码中找不到问题。它应该使用AJAX。我简化了所有内容,因此您可以更轻松地阅读。这是HTML表单:
<form id="fileForm" enctype="multipart/form-data" method="POST" action="php/uploadfile.php">
<p>Insert file: <input type="file" id="fileUp" name="fileUp" />
<button type="submit" id="uploadButton" onclick="sendFile();">Upload</button></p>
</form>
现在这里有javascript sendFile()功能:
function sendFile()
{
var forma = document.getElementById("fileForm");
var failas = document.getElementById("fileUp");
var uploadButton = document.getElementById("uploadButton");
forma.onsubmit = function(event)
{
event.preventDefault();
}
uploadButton.innerHTML = "Uploading, please wait!";
var newFile = failas.files[0];
var formData = new FormData(forma);
formData.append("fileUp", newFile, newFile.name);
alert(newFile.name);// Here it says filename.jpg it means everything is ok at this stage
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
alert (xmlhttp.responseText);
}
}
xmlhttp.open("POST", "php/uploadfile.php", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(formData);
}
和php:
<?php
echo var_dump($_FILES);
?>
它应该提醒$ _FILES的内容,但是即使我尝试$ _REQUEST,它也会显示数组(0){}。所以,如果有人能够提出可能出现的问题,我们将不胜感激:]