Setting a Limit to the Maximum Size of an Upload
Fredrik Lundh
fredrik at pythonware.com
Tue Oct 25 04:18:44 EDT 2005
"Joey C." wrote:
> Here is a basic overview of the variables included there.
>
> params = cgi.FieldStorage()
> I accidentally made a mistake when typing what the "thefile" variable
> is.
> thefile = params["upfile"].file
> "upfile" is the CGI field that contains the file that I'm uploading.
> As you can see, the if statement just compares two values,
> os.path.getsize(thefile) and conf["upmax"], a variable I set that is
> designated as the maximum file size allowed.
>
> I'm assuming that this is all the information you need. I'm sorry for
> not including it earlier; I was in a bit of a rush. ^.^
and I'm sorry for not noticing the error: os.path.getsize takes a filename,
but the file attribute contains a file handle, and that UnicodeError is what
you get if you pass the wrong thing to os.path.getsize. e.g:
>>> os.path.getsize(None) < 1000
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python23\lib\ntpath.py", line 228, in getsize
return os.stat(filename).st_size
TypeError: coercing to Unicode: need string or buffer, NoneType found
(here, the full traceback reveals that the problem is inside getsize, and not
in the comparision. the error message itself isn't exactly helpful, though...)
the file handle is usually a real (but temporary) file, so you should be able to
get the size by seeking to the end of the file:
file = params["upfile"].file
file.seek(0, 2)
size = file.tell()
file.seek(0) # rewind
inspecting the headers attribute might also help.
</F>
More information about the Python-list
mailing list