问题描述
我有一个记录音频并返回blob的站点,目标是通过fetch api或其他方式从客户端原始javascript发送该blob,然后将其发送到node.js服务器,然后从那里将其上传到特定目录,例如fs。
function audio() {
let blob = new Blob(chunks,{ 'type': 'audio/mp3;' });
// i want to send this blob to the back end server via fetch API
}
这是我的后端
//----------Upload
spokenBio.post('/upload-spoken-bio',async (req,res) => {
//waiting to get the blob or the file via req.body and somehow turn it into a real file write it
//into the server disk
})
解决方法
要发送,您可以使用Fetch API:
fetch('/upload-spoken-bio',{
method: 'POST',body: blob
});
在服务器上,您可以简单地执行以下操作:
req.pipe(
fs.createWritableStream('/some/path/file')
);
当然,请确保您信任此处的数据,并且不要让用户指定路径。