我有一个 Axis M1011 摄像头,只要它检测到运动,它就可以将一系列 jpeg 图像发送到服务(使用 HTTP POST)。我正在使用 node.js 构建服务。
我已成功接收带有标头的 POST 请求,但无法将数据保存在请求正文中。这是代码:
function addEvent(req, res)
{
var buffer = '';
console.log(req.headers);
req.on("data", function(chunk)
{
console.log("chunk received");
buffer += chunk;
});
req.on("end", function()
{
console.log("saving file");
fs.writeFile("./tmp/"+ new Date().getTime()+".jpg", buffer, function(error)
{
if(error)
{
console.log(error);
}
else
{
console.log("saved");
res.send("OK");
res.end();
}
});
});
}
在控制台上,我得到了这种输出。当然,内容长度因文件而异:
{ host: '192.168.0.100:8888',
'content-type': 'image/jpeg',
'content-disposition': 'attachment; filename="file13-07-19_20-49-44-91"',
'content-length': '18978' }
chunk received
chunk received
chunk received
chunk received
chunk received
chunk received
chunk received
chunk received
chunk received
chunk received
chunk received
chunk received
chunk received
chunk received
saving file
saved
问题是我在 tmp 文件夹中得到了一个相同的、损坏的文件,大小约为 33KB,无论图像有多大。接收这些文件我做错了什么?