我正在编写一个图片 uploader ,我想将图片的大小限制在 3mb 以下。在服务器端,我可以检查 header 中图像的大小,如下所示(使用 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...
}
我试图通过(和排列)停止请求
req.shouldKeepAlive = false;
req.client.destroy();
res.writeHead(200, {'Connection': 'close'});
res.end()
它们都没有真正“破坏”阻止上传更多数据的请求。 req.client.destroy() 似乎卡住了下载,但是 res.send({error... 没有被发回。
帮助!
最佳答案
抛出一个错误并捕获它。它将停止文件上传,允许您发送响应。
try { throw new Error("Stopping file upload..."); }
catch (e) { res.end(e.toString()); }
这有点hackish,但它确实有效......
关于file-upload - 在 NodeJs 中上传文件时停止请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907096/