使用fs将Blob上传到服务器

问题描述

我有一个记录音频并返回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')
);

当然,请确保您信任此处的数据,并且不要让用户指定路径。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...