web端录音生成的blob音频文件如何保存至服务器
blob数据
var blob = new Blob(e.data.buf, {type: 'audio/mp3'});
发送到blob (js处理)
var fd = new FormData();
var wavName = encodeURIComponent('audio_recording_' + new Date().getTime() + '.mp3');
fd.append('wavName', wavName);
fd.append('file', blob);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
jsonObject = JSON.parse(xhr.responseText);
}
};
xhr.open('POST', '/xxx/uploadVoice');
xhr.send(fd);
php保存文件 (thinkphp)
public function uploadVoice()
{
$file = $this->request->file('file');
if ($file) {
$newpath = ROOT_PATH . "/public/assets/upload/voices/";
$info = $file->move($newpath, time() . ".wav");
if ($info) {
$imgname = $info->getFilename();
$imgpath = $this->base_root . "/assets/upload/voices/" . $imgname;
$arr = [
'data' => [
'src' => $imgpath
]
];
return json_encode($arr);
} else {
return false;
}
}
}