I have been working on this problem for two days now, and I am stuck. I am using Node.js with Express, and I am trying to implement an upload form. Basically, I want the form to
Here is a solution that works for me using multiparty.
The answer given by jfizz does not work on all browsers.
VERY IMPORTANT: As josh3736 mentioned on a comment above, you MUST set the Connection header to close!
form.on('part', function(part) {
if (![some condition I want to be met]) {
form.emit('error', new Error('File upload canceled by the server.'));
return;
}
... code to handle the uploading process normally ...
});
form.on('error', function(err) {
console.log('ERROR uploading the file:',err.message);
res.header('Connection', 'close');
res.status(400).send({ status:'error' });
setTimeout(function() { res.end(); }, 500);
next(err);
});