在NodeJs中上传文件时停止请求 [英] Stop request during file upload in NodeJs
问题描述
我正在写一个图片上传器,我想将图片的大小限制在3mb以下。在服务器端,我可以检查标题中图像的大小,像这样(使用快递):
app .post('/ upload',function(req,res){
if(+ req.headers ['content-length']> 3001000){//约3mb
//做某事停止结果
返回res.send({'error':'某种错误'});
}
//在这里数据流...
}
我试图通过(和permations)停止req(
req.shouldKeepAlive = false;
req.client.destroy();
res.writeHead(200,{'Connection':'close' });
res.end()
没有一个真的销毁请求以防止更多的数据上传
req.client.destroy()似乎冻结下载,但res.send({错误...不被发送回来。
帮助!
抛出一个错误并捕获它。上传,允许您发送回复。
try {throw new Error(Stopping file upload ...); }
catch(e){res.end(e.toString()); }
这有点冒险,但是它可以工作...
I'm write an image uploader, and I want to constrain the size of the image to under 3mb. On the server side, I can check the size of the image in the header, something like this (using express):
app.post('/upload', function(req, res) {
if (+req.headers['content-length'] > 3001000) { // About 3mb
// Do something to stop the result
return res.send({'error': 'some kind of error'});
}
// Stream in data here...
}
I tried to stop the req by (and permuations of)
req.shouldKeepAlive = false;
req.client.destroy();
res.writeHead(200, {'Connection': 'close'});
res.end()
None of them really "destroys" the request to prevent more data being uploaded. req.client.destroy() seem to freeze the download, but the res.send({error... is not being sent back.
Help!
Throw an error and catch it. It will stop the file upload, allowing you to send a response.
try { throw new Error("Stopping file upload..."); }
catch (e) { res.end(e.toString()); }
It's a bit hackish, but it works...
这篇关于在NodeJs中上传文件时停止请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!