Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 无法将recorder.js record wav文件保存到node/express后端_Node.js_Audio_Multipartform Data - Fatal编程技术网

Node.js 无法将recorder.js record wav文件保存到node/express后端

Node.js 无法将recorder.js record wav文件保存到node/express后端,node.js,audio,multipartform-data,Node.js,Audio,Multipartform Data,我很难把线路连接起来 我正在使用,以下是我所拥有的: recorder.on('finishRecord', function(){ var formData = new FormData(); formData.append('file', recorder.recordedData); $http.post('/api/submit_record', formData, { // Using Angular... headers: {'Content-Type': 'a

我很难把线路连接起来

我正在使用,以下是我所拥有的:

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
中引用
文件
键,但我没有找到它的位置

我已经读了很多关于这个的例子,但是没有人能为我找到一个解决方案

我刚通过以下curl请求使后端工作,从:

我真的不介意数据是通过
FormData
、在
json
www-encoded
中发送的,我只想这样做就行了


提前谢谢

您不需要
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'))
});

您不需要
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'))
});

我甚至没想过要试试这个!谢谢你,伙计!我甚至没想过要试试这个!谢谢你,伙计!