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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With