【问题标题】:Uploading recorded audio to the node js server using Multer使用 Multer 将录制的音频上传到节点 js 服务器
【发布时间】:2019-05-23 10:42:25
【问题描述】:

我想使用 multer 将录制的音频文件上传到 Node js 服务器,但我没有在服务器上获取文件。这是我的代码。我该怎么办?

JS 客户端代码:

mediaRecorder.onstop=(e)=>{
          let blob=new Blob(chunks,{'type':'audio/mp3;'});
          chunks=[];
          let audioURL=window.URL.createObjectURL(blob);
          audio.src=audioURL;
          var data = new FormData();
          var request = new XMLHttpRequest();
          data.append('file',blob,'audio.mp3');
          request.open('post','/upload'); 
          request.send(data);
          console.log('File sent');
        }

服务器端代码:

app.post('/upload', upload.single('file'), function (req, res, next) {

          console.log(req.file); 
          var oldpath = req.file;
          var newpath = __dirname;
          fs.readFile(oldpath, function (err, data) {
          fs.writeFile(newpath, data, function (err) {
                if (err) throw err;
                res.end();
            });

  })
})

我现在收到此错误: See the output Here and Error

【问题讨论】:

  • @iambatman 谢谢,但我试过了,它不起作用
  • 你应该在xmlhttp请求中设置内容类型头
  • 这里是`request.setRequestHeader("Content-Type", "multipart/form-data");`

标签: javascript node.js file-upload multer mediarecorder-api


【解决方案1】:

在http请求中设置内容类型头

var request = new XMLHttpRequest();
data.append('sound',audioURL);

【讨论】:

  • 我收到此错误错误:多部分:未找到边界
  • 再试试我的答案
  • 你能在这里分享网络标签中的内容类型标题吗?并检查请求正文是否正确包含文件
【解决方案2】:
mediaRecorder.onstop=(e)=>{
      let blob=new Blob(chunks,{'type':'audio/mp3;'});
      chunks=[];
      let audioURL=window.URL.createObjectURL(blob);
      audio.src=audioURL;
      var data = new FormData();
      var request = new XMLHttpRequest();
      data.append('file',blob);
      request.open('post','/upload'); 
      request.send(data);
      console.log('File sent');
    }

这会起作用

【讨论】:

  • 得到这个:MulterError: Unexpected field
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 2021-07-12
  • 2019-04-24
  • 1970-01-01
  • 2017-08-30
相关资源
最近更新 更多