I've got a program that reads and sorts information from a CSV file but I can't find out how to upload a file and either read it directly (which I don't think is possible) or upload it to a server.
Everything I try to Google either doesn't work or seems too ambiguous.
Has anyone got any idea how to upload the file from an HTML form so that I can read it in the program?
To read the file I'm using the csv module:
readerCTR = csv.reader(open("/home/ctrdata.csv", "rb"))
I'm using a really basic html form:
<form action="test" method="post" enctype="multipart/form-data">
Upload file: <input type="file" name="myfile" /> <br />
<input type="submit" name="submit" value="Submit" />
</form>
and I've been trying to use the tutorial on CGI at docs.python
form = cgi.FieldStorage()
fileitem = form["myfile"]
if fileitem.file:
linecount = 0
while 1:
line = fileitem.file.readline()
if not line: break
linecount = linecount + 1
but I just get key errors.
KeyError: 'myfile'
It seems like it isn't getting passed through at all. If I check the debugger:
>>> form
FieldStorage(None, None, [])
None of this makes ANY sense to me. I've never had to upload files before. I do have a server that I could save it to if I need to, but it would be ideal if I could just read it and temporarily save the data.
Do you think it could be that I'm using Firefox and Linux?
</form>tag? If not, perhaps the browser is closing it automatically, so that the file input isn't contained in the form itself?