问题是无法通过 FormData 发送文件。
可用输入
<input id="Music" type="file">
添加监听器
Music.addEventListener("change", LoadMusic);
在 LoadMusic 我只给 src 和 MusicFile 我给文件
function LoadMusic(e) {
var Files = e.target.files;
var FileName = URL.createObjectURL(e.target.files[0]);
if (Files.length != 1) {
alert("Файл должен быть 1");
} else if (Files[0].name.substr(Files[0].name.length - 4) != ".mp3")//Add check Case
{
alert("Поддерживается только mp3");
} else {
//Player this document.getElementByID("Music")
Player.src = FileName;
Player.load();
MusicFile = e.target.files[0];
}
}
在某些操作之后,我调用 SendResult 函数
function SendResult() {
var Data = new FormData();
var TestArray = ArrayNodes;
for (var i = 0; i < NumElem; i++) {
delete TestArray[i]._Node;
delete TestArray[i]._Pick;
}
var js = (JSON.stringify(TestArray));
Data.append("LevelJSON",js);
Data.append("fil",MusicFile);
Data.append("UserName",ClientName);
var NameLevel = document.getElementById("NameLevel");
Data.append("LevelName",NameLevel.value);
var xhr = new XMLHttpRequest();
xhr.open('post', 'Utilis/SendMap.php',true);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.onreadystatechange = function() {
if (this.readyState != 4) return;
console.log(this.responseText);
}
xhr.send(Data);
xhr = null;
}
PHP - 文件包含
if (isset($_POST['LevelJSON'])){
$Path=$_SERVER['DOCUMENT_ROOT'] ."/Maps/".$_POST['UserName']."/".$_POST['LevelName']."/";
echo($_POST['LevelJSON']);
mkdir($Path,0777,true);
$File = fopen($Path.$_POST['LevelName'].".json", "w");
fwrite($File, "var Data = '".$_POST['LevelJSON']."';");
fclose($File);
}else{
echo($_FILES['fil']['tmp_name']);
}
输出响应....
<br />
Notice : Undefined index: fil in /var/www/html/Utilis/SendMap.php on line 16
(即POST请求中没有LevelJSON) 哪里出错了?
xhr.setRequestHeader("Content-Type", "multipart/form-data"); 中可能存在错误
也许问题是这样的:
问题出在 MAX_UPLOAD (php.ini)