Hello, Does a FileUpload object have a get_size() method? I'd like to limit the size of an uploaded file before commiting it to the ZODB. I've been Googling for a while. Is it handled differently on a FileUpload object. I'm planning to process with a Python Script. Could someone point me in the right direction? Thanks In Advance, -Jon Cyr cyrj@cyr.info -- Jonathan Cyr cyrj@cyr.info
Am Dienstag, den 25.10.2005, 14:43 -0400 schrieb Jonathan Cyr:
Hello,
Does a FileUpload object have a get_size() method?
I'd like to limit the size of an uploaded file before commiting it to the ZODB. I've been Googling for a while. Is it handled differently on a FileUpload object.
I'm planning to process with a Python Script.
Could someone point me in the right direction?
The fileupload object is simply a file. (can be cStringIO, can be tempfile - depending on size) Best is to do so: maxsize=1<<20 # 1MB fu=context.REQUEST.get('fileupload') fu.seek(maxsize) if fu.read(1): raise OverflowError("File too big!") # or something like that fu.seek(0) # rewind to start filefolder.manage_addFile(...) This avoids loading the whole file into memory to find out. HTH Tino
I think I get it, except... What's the meaning of 1<<20, I'm not familar with that operator. How would I adjust it for different sizes? ... as I instantly regret blowing-off bit operators, as "something I wont need"... -Jon Tino Wildenhain wrote:
Am Dienstag, den 25.10.2005, 14:43 -0400 schrieb Jonathan Cyr:
Hello,
Does a FileUpload object have a get_size() method?
I'd like to limit the size of an uploaded file before commiting it to the ZODB. I've been Googling for a while. Is it handled differently on a FileUpload object.
I'm planning to process with a Python Script.
Could someone point me in the right direction?
The fileupload object is simply a file. (can be cStringIO, can be tempfile - depending on size)
Best is to do so:
maxsize=1<<20 # 1MB
fu=context.REQUEST.get('fileupload') fu.seek(maxsize)
if fu.read(1): raise OverflowError("File too big!") # or something like that
fu.seek(0) # rewind to start
filefolder.manage_addFile(...)
This avoids loading the whole file into memory to find out.
HTH Tino
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
-- Jonathan Cyr cyrj@cyr.info
Am Dienstag, den 25.10.2005, 16:42 -0400 schrieb Jonathan Cyr:
I think I get it, except...
What's the meaning of 1<<20, I'm not familar with that operator. How would I adjust it for different sizes?
Well, you could (and should!) study the python documentation. But this one is only an example. You can write the maximum however you want, as literal value: 1000000 or 1e+6 or 1000*1000 or 10**6 ... If you have an imagination of what value you want, just write it there :-)
Thanks for your time, much appreciated. -Jon Tino Wildenhain wrote:
Am Dienstag, den 25.10.2005, 16:42 -0400 schrieb Jonathan Cyr:
I think I get it, except...
What's the meaning of 1<<20, I'm not familar with that operator. How would I adjust it for different sizes?
Well, you could (and should!) study the python documentation. But this one is only an example. You can write the maximum however you want, as literal value: 1000000 or 1e+6 or 1000*1000 or 10**6 ...
If you have an imagination of what value you want, just write it there :-)
-- Jonathan Cyr cyrj@cyr.info
participants (2)
-
Jonathan Cyr -
Tino Wildenhain