Hi,

The sad answer is that you can not prevent the actual data transfer between
the browser and the server. The core problem is that all browsers of today
will only read the response after the request has been fully written out.
This means that even if you know the size from the content-length and you
know that you do not want to process the inputstream at all, the data will
still be sent by the browser and the servlet container will still have to
read and skip it.

The only exception to this is a new settings in tomcat 7.0.11:
"swallowAbortedUploads" (see [1]). If you disable this (default is true)
Tomcat will not read the request body if the application set a 413 response
(request entity too large). This is quick and prevents the data transfer but
all browsers treat it as an aborted connection during request sending (and
they do not care about the response already sitting there at their sockets)
and they show a big error page.
However this can probably still be worked around if you target a hidden
iframe with the upload form. So if the file is big and tomcat closes the
connection the error is loaded into the iframe and it is not shown to the
user. Maybe this could be integrated into the wicket-ajax-multipart-upload
functionality but currently it is not. I would definitely like it (and I
already opened an improvement issue not long ago about this, see [2]).

[1] "swallowAbortedUploads":
http://tomcat.apache.org/tomcat-7.0-doc/config/context.html
[2] https://issues.apache.org/jira/browse/WICKET-3525

<http://tomcat.apache.org/tomcat-7.0-doc/config/context.html>Attila

2011/3/18 Henrique Boregio <hbore...@gmail.com>

> Hi, how can I restrict the size of a file being uploaded, before it gets
> uploaded?
>
> The following code does not work, since wicket first uploads the file to
> the
> server, THEN checks its size (I've tried it uploading a 1Gig file):
>
>       protected void onSubmit() {
>
>            final FileUpload upload = fileUploadField.getFileUpload();
>
>            if (upload != null) {
>             if(upload.getSize() > Bytes.kilobytes(150).bytes()) {
>             return;
>             }
> }
> }
>
>
> I've also tried setting the form's maxSize like so, but the file still
> seems
> to get uploaded:
> setMaxSize(Bytes.kilobytes(150));
>
> Anyone run into this same problem? Thanks.
>

Reply via email to