Adrian Holovaty wrote:
def isSmallFile(self, field_data, all_data):
if len(field_data["content"] > 10000: # 10,000 bytes
raise validators.ValidationError, "Please enter a smaller file."
The trick here is to operate on field_data['content'] instead of
field_data. File-upload fields are a special-case; they come across as
a dictionary of {'filename', 'content-type', 'content'}.
If you can do len(filed_data['content']) then this content is already
has been uploaded on the server. At present the only way to know the
size of data before they are uploaded is the Content-Length header sent
by browsers. It also depends on server environment if it passes the
control to a script before all the POST data has been received. I have
an impression that Apache doesn't do it (thought I didn't check
properly). And even if it did then the only way to prevent user to
download anything is to break the connection. Which also generally
impossible with http servers and would look ulgy in a browser that just
sends data and doesn't expect anything sane to happen before upload
finishes. So it would just show some network error message.
I saw some discussions about specifying content size restriction in
future browsers on a WHAT WG list
(http://listserver.dreamhost.com/pipermail/whatwg-whatwg.org/) but it
didn't end up in anything yet.