【发布时间】:2019-11-06 12:49:07
【问题描述】:
所以我使用 recorder.js 来捕获一些音频,然后我使用此代码将音频上传到我们的服务器:
var url = URL.createObjectURL(blob);
var au = document.createElement('audio');
var li = document.createElement('li');
var link = document.createElement('a');
//name of .wav file to use during upload and download (without extendion)
var filename = "test";
//add controls to the <audio> element
au.controls = true;
au.src = url;
//save to disk link
link.href = url;
link.download = filename+".wav"; //download forces the browser to donwload the file using the filename
link.innerHTML = "Save to disk";
//add the new audio element to li
li.appendChild(au);
//add the filename to the li
li.appendChild(document.createTextNode(filename+".wav "))
//add the save to disk link to li
li.appendChild(link);
//upload link
var xhr=new XMLHttpRequest();
xhr.onload=function(e) {
if(this.readyState === 4) {
console.log("Server returned: ",e.target.responseText);
Redirect();
}
};
var fd=new FormData();
fd.append("audio_data",blob, filename);
xhr.open("POST","upload_audio.php",false);
xhr.send(fd);
这里是 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, "../data/".$output)
?>
如果您在 30 秒内进行录制,则此脚本可以完美运行。如果录音变大,我们会从服务器获得以下响应:
Server returned:
Array
(
[audio_data] => Array
(
[name] => QA_3_Issue,html,7fac7e13c55a4eb18b0f2160f8581097_7fac7e13c55a4eb18b0f2160f8581097
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
有人知道发生了什么吗?可能是服务器问题? 我也不太明白为什么它说错误,但没有说明错误到底是什么。
最好的问候, 丹尼尔
【问题讨论】:
-
你能检查你的 php.ini 上的 'upload_max_filesize' 吗?
-
谢谢,伙计们,这确实是问题所在。我还是 PHP 新手。