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 my solution:
var maxSize = 30 * 1024 * 1024; //30MB
app.post('/upload', function(req, res) {
var size = req.headers['content-length'];
if (size <= maxSize) {
form.parse(req, function(err, fields, files) {
console.log("File uploading");
if (files && files.upload) {
res.status(200).json({fields: fields, files: files});
fs.renameSync(files.upload[0].path, uploadDir + files.upload[0].originalFilename);
}
else {
res.send("Not uploading");
}
});
}
else {
res.send(413, "File to large");
}
And in case of wasting client's uploading time before get response, control it in the client javascript.
if (fileElement.files[0].size > maxSize) {
....
}