2017-02-10 71 views
0

我很难一起插入线条。不能保存recorder.js记录wav文件到节点/快递后端

我使用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(); 
    }); 
}); 

但我的文件是不是在年底可读......

我知道我应该在formDatafile密钥的引用,但我没有找到在哪里。

一直念叨许多这样的例子,但没有人找到了一个解决方案,我...

我只是做了我的后端的工作有以下卷曲请求从there

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

我真不不介意数据是通过FormData发送的,还是jsonwww-encoded发送的,我只是想让它工作。

在此先感谢!

回答

2

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

我甚至没有想到尝试这种之一!谢了哥们! –