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?
The problem with this was that I was using Pylons, so it was almost ignoring the CGI.
So instead I should have been using:
request.POST['myfile'].value
That you get the error means the upload works - sort of: Your CGI script is called but there is something wrong with the parameters. Steps to debug this:
Print the form.keys()
to sys.stdout
. That should give you a list of keys so you can check for typos.
Check the version of your cgi
module. The documentation says
and the latest addition is support for file uploads from a form
(my emphasis). Maybe your version is too old.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.