Upload size limit

1,387 views
Skip to first unread message

Tiziano Rossi

unread,
Feb 13, 2012, 1:32:53 PM2/13/12
to Express
Hi to all!

Using req.files (and not external formidable) for uploading image to
server, there is a way to limit the upload size?
I search in formidable options but i not see something like
maxFileSize... I search an options to put in express middleware (like
keepExtensions for example) that raise an error when file size exceed
the limit...

I know that there is a property size (req.files.<propertyname>.size),
but after uploaded is too late...

Thanks to all in advance!
Tiziano.

Dmitriy Petrov

unread,
Feb 13, 2012, 3:16:19 PM2/13/12
to expre...@googlegroups.com

Tiziano Rossi

unread,
Feb 13, 2012, 3:53:16 PM2/13/12
to Express
Thanks Dmitriy,

it seems a good solution... I just tried this, but how can i send back
to user a response, like "Max file size exceeed".. ???
There is no error event triggered, and the error middleware is not
called...

Probably newby question...


On 13 Feb, 21:16, Dmitriy Petrov <i.am.cor...@gmail.com> wrote:
> Hi,
>
> There is connect middlewarehttp://www.senchalabs.org/connect/middleware-limit.html

Linus G Thiel

unread,
Feb 14, 2012, 7:05:40 AM2/14/12
to expre...@googlegroups.com
Tiziano,

Looking at the source for connect-limit, it looks like if the client
sends a content-length, then if that exceeds your limit, the
middleware will respond with a 413, "Request Entity Too Large". If the
client doesn't send a content-length, or sends a larger request body,
then the request is silently destroyed.

> --
> You received this message because you are subscribed to the Google Groups "Express" group.
> To post to this group, send email to expre...@googlegroups.com.
> To unsubscribe from this group, send email to express-js+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/express-js?hl=en.
>

--
Linus G Thiel
Hansson & Larsson
http://hanssonlarsson.se/
+46 709 89 03 85

tjholowaychuk

unread,
Feb 14, 2012, 11:17:13 AM2/14/12
to Express
you can do something like (at the bottom of your middleware config):

app.use(function(err, req, res, next){
if (err.status == 413) {
res.status(413);
res.render('my-error-page', { msg: 'Max file size exceeded' });
} else {
next(err);
}
});
> > For more options, visit this group athttp://groups.google.com/group/express-js?hl=en.
>
> --
> Linus G Thiel
> Hansson & Larssonhttp://hanssonlarsson.se/

Ivan Stoyanov

unread,
Feb 14, 2012, 11:39:34 AM2/14/12
to expre...@googlegroups.com
Also if you want to really optimize you can add a handler for the checkContinue event (http://nodejs.org/docs/v0.4.11/api/http.html#event_checkContinue_), looking at the content-length again. This way the client won't send any data at all before you say it is ok via 100-Continue (assuming the client complies).
Reply all
Reply to author
Forward
0 new messages