我在将音频文件上传到服务器端页面时遇到问题。这是代码。我可以记录和下载文件,但问题是我无法将以下文件上传到upload.php。我目前正在使用xampp。谁能帮我吗? Javascript:
var upload = document.createElement('a');
upload.href="#";
upload.innerHTML = "Upload";
upload.addEventListener("click", function(event){
var xhr=new XMLHttpRequest();
xhr.onload=function(e) {
if(this.readyState === 4) {
console.log("Server returned: ",e.target.responseText);
}
};
var fd=new FormData();
fd.append("audio_data",blob, filename);
xhr.open("POST","upload.php",true);
xhr.send(fd);
});
li.appendChild(document.createTextNode (" ")); //add a space in between
li.appendChild(upload); //add the upload link to li
//add the li element to the ol
recordingsList.appendChild(li);
}
php:
<?php
print_r($_FILES); //this will print out the received name, temp name, type, size, etc.
$size = $_FILES['audio_data']['size']; //the size in bytes
$input = $_FILES['audio_data']['tmp_name']; //temporary name that PHP gave to the uploaded file
$output = $_FILES['audio_data']['name'].".wav"; //letting the client control the filename is a rather bad idea
//move the file from temp name to local folder using $output name
move_uploaded_file($input, $output)
?>
html:
<div id="controls">
<button id="recordButton">Record</button>
<button id="pauseButton" disabled>Pause</button>
<button id="stopButton" disabled>Stop</button>
</div>
<div id="formats">Start</div>
<h3>Recordings</h3>
<ol id="recordingsList"></ol>