我想通过 ajax 将文件发送到 php 我有下面的代码但是当我运行它时,它说 Undefined index: thefile 有什么问题? HTML
function postData(url){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var res = xmlHttp.responseText;
document.getElementById("upLoadName").textContent=res;
}
}
var formData = new FormData();
formData.append("thefile", document.getElementById('thefile').files[0]);
xmlHttp.send(formData);
}
和形式:
<form action="#" >
<input type="file" name="thefile" id="thefile"/>
<input type="button" name="Send" value="send" onclick="postData('upLoad.php');"/> </form>
PHP
echo json_encode($_FILES["thefile"]["name"]) ;