从设备向 node.js 中的服务器发送多部分请求 [英] send a multipart request from a device to server in node.js
问题描述
我从设备接收视频字节,它应该是一个多部分请求.它是在 node.js 中创建的 post api.如何在 node.js 的 post api 中接收多部分请求.以下是当前保存视频字节的代码.
I am receiving a video bytes from a device, and it should be a multipart request. Its a post api which is created in node.js. How can I recieve a multipart request in post api in node.js. Below is the code which is currently saving video bytes.
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);
推荐答案
你需要在 video bye 字符串后附加一个分隔符,如下所示
You need to append a separator after video bye string like following
如果字节字符串是 - 'abcdefghijklmnopqrstuvwxyz';
然后你需要像这样附加任何分隔符(#####)来标识完整字符串的结尾
then you need to append any separator(#####) like this to identify end of complete string
现在您的请求字符串将是这样的 - 'abcdefghijklmnopqrstuvwxyz'+'#####';
现在你的请求字符串有一个结束分隔符(#####),你可以从中识别你的完整块
Now your request string having a end separator(#####) from which you can identify your complete chunks
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);
}
谢谢
这篇关于从设备向 node.js 中的服务器发送多部分请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!