Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you restrict the size of a file being uploaded with JavaScript (or Java) without transferring the entire file?

Is there a way to validate on the client side browser whether the size of a file being uploaded from a JSP page is over a set size limit without forcing the user to upload the entire file only to find out it was too large?

I would like to stay away from any proprietary controls or techniques like Flash or ActiveX if possible.

Thanks!

like image 458
coleca Avatar asked Oct 01 '08 15:10

coleca


2 Answers

This isn't a perfect solution, but if you check the Content-Length HTTP header with request.getHeader("Content-Length") then you can choose to not transfer the entire file.

By way of explanation, an extremely large file will not be transferred all at once. You'd have to actually open a stream representing that chunk of POST data and read from it for the entire thing to be transfered.

On the other hand, if you're worried about denial-of-service attacks, then you can't really trust the Content-Length header, because it can easily be forged. In this case, you should set a limit and stream a transfer of this file, stopping as soon as you've exceeded that limit.

like image 110
Eli Courtwright Avatar answered Oct 22 '22 07:10

Eli Courtwright


Suggest you reconsider the Flash decision and take a look at the YUI Uploader, here:

http://developer.yahoo.com/yui/uploader/

Among other things, the fileSelect event will tell you the size of the selected file in bytes immediately after it is selected but before it's uploaded, so you'll be able to restrict accordingly.

like image 28
Kent Brewster Avatar answered Oct 22 '22 07:10

Kent Brewster