Mailing List Archive

upload file with cgi
I am uploading a file using the cgi module and I can not figure out how
to get the name of the file.
The code I am using is
form=cgi.FieldStorage()
pic=form["picture"]
Now when I print pic it gives me the name of the File and the actual
file.
When I get pic.value it is the actual picture.
pic.key doesn't give me anything and I have absolutely no clue how to
get the filename.

Please help.

Sim
upload file with cgi [ In reply to ]
Sim & Golda Zacks <simngolda@voyager.net> wrote:
: I am uploading a file using the cgi module and I can not figure out how
: to get the name of the file.
: The code I am using is
: form=cgi.FieldStorage()
: pic=form["picture"]
: Now when I print pic it gives me the name of the File and the actual
: file.
: When I get pic.value it is the actual picture.
: pic.key doesn't give me anything and I have absolutely no clue how to
: get the filename.

: Please help.

There should be a "filename" attribute along with the "file" object.
print 'You gave me the file:', pic.filename

-Arcege
upload file with cgi [ In reply to ]
Hi,

This must be a FAQ question, but I'm wondering where the file disappears
from FieldStorage? My own cgi-upload script used to work but now what
happens is that the FieldStorage is empty. Is this just some silly mistake
or does it have something to do with netscape?

<FORM ACTION="/cgi-bin/upload.py" method=POST enctype=multipart/form-data>
<p>Upload a file!</p>
<SELECT NAME=dest>
<OPTION value=koulutus selected>koulutus
<OPTION VALUE=tiedotus>tiedotus
</SELECT>
<INPUT TYPE="file" name="userfile" SIZE="30">
<INPUT TYPE=SUBMIT value=SEND!>
</FORM>


An excerpt from the script:

form = cgi.FieldStorage()
print form
sys.stdout.flush()
fileitem = form['userfile']
if fileitem.file and form.has_key('dest'):
Upload(fileitem, form['dest'].value)

and what 'print form' prints if I send the form with a file selected:
FieldStorage(None, None, [])

and, of course, an exception is raised
Traceback (innermost last): File "/usr/lib/cgi-bin/upload.py", line 53, in
Main fileitem = form['userfile'] File"/usr/lib/python1.5/cgi.py", line 907,
in __getitem__ raise KeyError, key KeyError: userfile

I'm confused. Please help.

Sami