[mod_python] File upload

David Fraser davidf at sjsoft.com
Thu Sep 18 09:47:24 EST 2003

Greenbeard wrote:

>Does anyone have a good file upload example?  
>When I try something like: 
>def upload(req, file):
>    tempFile = file.read()
>I get: AttributeError: File instance has no attribute
>if I use:
>    tempFile = req.form['file'] 
>I get the file contents but I don't think this would
>work well with large files (or would it?) 
>when I try to get attribues like file.type I get 
>AttributeError: File instance has no attribute 'type'.
>I do get the file.filename.   
>So how can I tell if it should be a binary write etc.?
>It looks like the publisher handler (which I am using)
>returns a File object but I can not get it to act like
>a file/StringIO object nor can I accesss the
>attributes that are mentioned in the documentation.   
>I am using W2k, Python 2.2  and Mod_py 3.0.3.  
>Thanks for the help,
>Do you Yahoo!?
>Yahoo! SiteBuilder - Free, easy-to-use web site design software
>Mod_python mailing list
>Mod_python at modpython.org
Don't know much about publisher, but a normal mod_python handler should 
be able to do something like this (totally untested code):

def handle(req):
  argdict = util.FieldStorage(req)
  for key in argdict.keys():
    value = argdict[key]
    if type(value) == types.InstanceType:
      if isinstance(value, util.StringField):
        # this is for a normal field...
      elif isinstance(value, util.Field):
        # this is for an attachment upload...
        filename = field.filename
        content_type = field.type
        # this will work by doing a read...
        contents = field.value
        # or you can do the read yourself:
        contents = field.file.read()
        # reset the position so others can read it

More information about the Mod_python mailing list