[mod_python] how do I uplaod a file - code snippets sought

Dave Britton dave at davebritton.com
Sat Oct 14 10:30:36 EDT 2006


What is the way to use mod_python.utils to get a file object create and uploaded from an html form? I can't quite get my head around it and I can't find any useful snippets of code to copy. I'd appreciate any help, especially an example. Thanks!

Say I have an html form like this:
 """<form action="upload" method="POST" enctype="multipart/form-data">
 File name: <input name="file" type="file"><br>
 <input name="submit" type="submit" value = "Send this file to the web server">
 </form>"""

What does upload.py have to do to process the file whose name is submitted?

the only cookbook version I have found is for  regular python in cgi mode:    
==================
 form = cgi.FieldStorage()
    if not form.has_key(form_field):
        print HTML_TEMPLATE2% {'SCRIPT_NAME':os.environ['SCRIPT_NAME']}
        return
    fileitem = form[form_field]
    if not fileitem.file:
        return
    fname=os.path.basename(fileitem.filename)
    print "fileitem.filename=%s,<br> uploadddir=%s,<br> fout=%s" \
       %(fileitem.filename, upload_dir, os.path.join(upload_dir, fname) )
    
    fout = file (os.path.join(upload_dir, fname), 'wb')
    while 1:
        chunk = fileitem.file.read(100000)
        if not chunk: break
        fout.write (chunk)
    fout.close()
===============    
How do I adapt this to use mod_python FieldStorage ? Especially for early mod_python as the installation I need this for has version 2.7 on Apache 1.3, debian linux

Dave Britton
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20061014/90932297/attachment-0001.html


More information about the Mod_python mailing list