我正在编写一个图像上传器,我想将图像的大小限制在 3mb 以下。在服务器端,我可以检查标题中图像的大小,如下所示(使用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... 没有被发回。
Help!
抛出错误并捕获它。它将停止文件上传,允许您发送响应。
try { throw new Error("Stopping file upload..."); }
catch (e) { res.end(e.toString()); }
这有点黑客,但它有效......
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)