我很难将线连接在一起。
我正在使用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();
});
});
但是最后我的文件不可读...
我知道应该以某种方式引用
file
中的formData
键,但是我找不到位置。在阅读有关此的许多示例,但没有人为我找到解决方案...
我只是使用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
我真的不介意数据是通过
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'))
});
关于node.js - 无法将Recorder.js记录WAV文件保存到 Node/表达后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42166711/