|
Tom Emerson
tree at basistech.com
Thu Apr 11 16:14:14 EST 2002
Christopher Sean Hilton writes:
> I'm trying to create a script to handle a file upload from a form with
> mod_python. I know that I should be looking into the util module but I've
> really got no clue about how to get started. Has anyone got some words of
> wisdom to put me on the right track?
I've done this using publisher: it's certainly the easiest way.
Given a form along the lines of:
<form action="process.py/doit" method="POST" enctype="multipart/form-data">
<b>Filename</b>: <input type="file" name="file">
</form>
And a form handler "doit" in process.py:
def doit(req, file):
# ...
The 'file' argument is a file object representing the uploaded file:
you can read from this as you would any other file.
You should check to make sure that the file has a non-zero length:
import os, stat
statinfo = os.fstat(file.fileno())
if statinfo[stat.ST_SIZE] > 0:
# process the text
else:
# it's an empty file, or it didn't exist!
You should also look at the content type of the uploaded file. You can
access this via the form table in the request record:
_file = req.form['file']
if _file.type == "text/plain":
# process plain text
That should be enough to get you started, I expect.
-tree
--
Tom Emerson Basis Technology Corp.
Sr. Computational Linguist http://www.basistech.com
"Beware the lollipop of mediocrity: lick it once and you suck forever"
|