views:

56

answers:

1

Hi, Whats wrong in this code?

Here is my HTML:

<html><body>
<form action="iindex.py" method="POST" enctype="multipart/form-data">
<p>File: <input type="file" name="ssfilename"></p>
<p><input type="submit" value="Upload" name="submit"></p>
</form>
</body></html>

This is my Python script:

#! /usr/bin/env python
import os, sys;
from mod_python import apache
import cgi
import cgitb; cgitb.enable()

form = cgi.FieldStorage(keep_blank_values=1)
fileitem = form["ssfilename"]
.....

This is the line where I get KeyError.

File "/Applications/MAMP/python/framework/Python.framework/Versions/2.6/lib/python2.6/cgi.py", line 541, in __getitem__
raise KeyError, key

KeyError: 'ssfilename'
A: 

Edit: Totally missed the part where you are doing keep_blank_values = 1; sorry, no idea what is wrong.

From http://docs.python.org/library/cgi.html:

Form fields containing empty strings are ignored and do not appear in the dictionary; to keep such values, provide a true value for the optional keep_blank_values keyword parameter when creating the FieldStorage instance.

Therefore, this is happening because this field was left blank.

orangeoctopus
yes, I still cant figure out why I get the Keyerror.Could there be something else going wrong?
ssdesign