I'm going throw file upload on nodejs with express.
I see that bodyParcer gets the job done...
app.use(express.bodyParser({"limit": '2mb'}));
but if i want to limit the size of the request i found that it dosen't cancel the upload some how. The client keeps sending data.
So i create this middleware...
app.use(function (err, req, res, next) {
if(err.status == 413){
req.destroy();
return res.json({
"status": 413,
"message": err
},413);
}else
next(err);
});
it works, cancel de upload but the client dosen't get(or ignore) the response!
i think this could be a behavior of the http protocol, so any help is appreciated.
I think that your code should work, but don't destroy the request! Just delete req.destroy();
line from your code.
You can also try something like this (it was working on my side) in order not to use middleware:
express = require 'express'
app = express()
swig = require 'swig'
cons = require 'consolidate'
swig.init {
root: __dirname + '/views'
allowErrors: true
}
app.set 'views', __dirname + '/views'
app.set 'view options', {layout: false}
app.engine '.html', cons.swig
app.set 'view engine', 'html'
# set upload dir
app.use express.bodyParser {uploadDir: './upload'}
app.get '/', (req, res) ->
res.render 'index.html', {}
app.post '/file-upload', (req, res) ->
# you can get some info about files here (size, path, name, type)
console.log req.files
# check that size is good
if req.files.thumbnail.size > 200
res.send 413
else
res.send 'Uploaded!'
console.log 'listen port 3000'
app.listen 3000
And in form:
<form method="post" enctype="multipart/form-data" action="/file-upload">
<input type="file" name="thumbnail">
<input type="submit">
</form>
Look here for details. Hope this will help you.