我从设备接收视频字节,它应该是一个多部分请求。它是一个在node.js中创建的post api。如何在node.js中的post api中接收多部分请求。以下是当前正在保存视频字节的代码。
app.js
app.post('/saveVideo',service.saveVideo);
routes.js(class service method saveVideo)
var video_byte_string = req.param('videoByteStr');
var writeStream = gfs.createWriteStream({mode:'w',content_type: 'video/mov'});
var buffer = new Buffer(video_byte_string).toString('base64');
var response = streamifier.createReadStream(buffer).pipe(writeStream);
答案 0 :(得分:0)
您需要在视频字符串之后添加分隔符,如下面的
如果字节字符串是 - 'abcdefghijklmnopqrstuvwxyz';
然后你需要附加这样的任何分隔符(#####)来识别完整字符串的结尾
现在你的请求字符串将是这样的 - 'abcdefghijklmnopqrstuvwxyz'+'#####';
现在您的请求字符串有一个结束分隔符(#####),您可以从中识别完整的块
var video_byte_string = req.param('videoByteStr');
isCorrectString = false;
data_stream += video_byte_string;
var n = data_stream.lastIndexOf("#####");
if (n === -1) {
var steamArr= data_stream.split("#####");
var completeByteString = steamArr[0];
isCorrectString = true;
}
if(isCorrectString) {
var writeStream = gfs.createWriteStream({mode:'w',content_type: 'video/mov'});
var buffer = new Buffer(completeByteString).toString('base64');
var response = streamifier.createReadStream(buffer).pipe(writeStream);
}
由于