[mod_python] Upload files to server?

Byron Ellacott bje at apnic.net
Wed Nov 3 18:32:19 EST 2004


Juliano Freitas wrote:
> How can i do a upload function to copy a local image files to a server??
> can i do that using cgi??

You need to have an appropriate form field on your page, with 
type="file".  You need to have that form submit using method="POST", 
since you cannot deliver a file's contents via GET.  You also need to 
set the form's enctype="multipart/form-data" to allow the file(s) to be 
encoded properly.  Example:

-----
<form action="upload.py" method="POST" enctype="multipart/form-data">
     File to upload: <input name="Image" type="file">
     <input type="submit" value="Go!">
</form>
-----

Then, you can have code to handle this:

from mod_python import apache, util

def handler(req):
     fs = util.FieldStorage(req)
     img = fs.getfirst('Image')
     print "Content-Type: text/plain\n\n"
     print "Filename: %s\n" % img.filename
     print "Content-Type: %s\n" % img.type
     data = img.value
     print "Content-Length: %s\n" % len(data)
     return apache.OK

-- 
bje


More information about the Mod_python mailing list