我有一個Axis M1011相機,它設置爲只要檢測到運動,就會向服務(使用HTTP POST)發送一系列jpeg圖像。我正在使用node.js構建服務。使用node.js接收POST請求
我成功地接收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();
}
});
});
}
在控制檯上,我得到了這種輸出。 Ofcourse,內容長度從文件的不同而不同文件:
{ 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
的問題是,我得到一個相同的,損壞的,其中大小約爲33KB,不管有多大的圖像tmp文件夾的文件。我在接收這些文件時做錯了什麼?
你知道如何解決這個問題嗎? – bubakazouba
說實話,沒有。我之後使用過node.js,我想我實際上已經開始工作,但在這種情況下,我不知道哪裏出了什麼問題。也許現有的答案有意義,但我沒有解決。 –