【问题标题】:Cannot save recorder.js record wav file to node/express backend无法将 recorder.js 记录 wav 文件保存到 node/express 后端
【发布时间】:2017-02-10 18:55:09
【问题描述】:

我很难将线路连接在一起。

我正在使用videojs-record,这就是我所拥有的:

recorder.on('finishRecord',  function(){
  var formData = new FormData();
  formData.append('file', recorder.recordedData);
  $http.post('/api/submit_record', formData, { // Using Angular...
    headers: {'Content-Type': 'audio/wav'}
  });
});

然后在服务器端:

let bodyParser = require('body-parser');
let app = express();

app.post('/api/submit_record', bodyParser.raw({ type: 'audio/wav', limit: '1mb' }), (req, res) => {
  console.log(req.body);
  fs.writeFile('public/myFile.wav', req.body, function(err) {
    console.log('File uploaded', fileName);
    res.write('File saved');
    res.end();
  });
});

但我的文件最后不可读...

我知道我应该在formData 中引用file 键,但我没有找到在哪里。

阅读了很多关于此的示例,但没有人找到适合我的解决方案...

我刚刚使用来自there 的以下 curl 请求使我的后端工作:

curl -X POST --data-binary @"public/sounds/1c0334bff518849e00aadd754b1a94f0.wav" -H "Content-Type: audio/wav" http://192.168.99.100:3000/api/submit_record

我真的不介意数据是通过FormDatajson 还是www-encoded 发送的,我只想这样工作。

提前致谢!

【问题讨论】:

    标签: node.js audio multipartform-data


    【解决方案1】:

    您不需要FormData 实例。这仅适用于您尝试发送的不仅仅是一件事情。将您的客户端请求更改为:

    recorder.on('finishRecord',  function(){
      $http.post('/api/submit_record', recorder.recordedData, {
        headers: {'Content-Type': 'audio/wav'}
      });
    });
    

    然后您可能可以将您的服务器端代码更改为:

    app.post('/api/submit_record', (req, res) => {
      req.pipe(fs.createWriteStream('public/myFile.wav'))
        .on('error', (e) => res.status(500).end(e.message))
        .on('close', () => res.end('File saved'))
    });
    

    【讨论】:

    • 我什至没有想过要尝试这个!谢谢老哥!
    猜你喜欢
    • 2014-03-12
    • 2017-11-24
    • 1970-01-01
    • 2016-01-22
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    相关资源
    最近更新 更多