用于上传 WAV blob 的客户端 JavaScript 函数:
function upload(blob) {
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("that_random_filename.wav",blob);
xhr.open("POST","<url>",true);
xhr.send(fd);
}
PHP 文件upload_wav.php
:
<?php
// get the temporary name that PHP gave to the uploaded file
$tmp_filename=$_FILES["that_random_filename.wav"]["tmp_name"];
// rename the temporary file (because PHP deletes the file as soon as it's done with it)
rename($tmp_filename,"/tmp/uploaded_audio.wav");
?>
之后您可以播放该文件/tmp/uploaded_audio.wav
。
但要记住!/tmp/uploaded_audio.wav
由用户创建www-data
,并且(在 PHP 默认情况下)用户不可读。要自动添加适当的权限,请附加该行
chmod("/tmp/uploaded_audio.wav",0755);
到 PHP 的末尾(在 PHP 结束标记之前?>
)。
希望这可以帮助。