Simple CSV (or file) upload and parse with GAE
I struggled the past few weeks with an odd bug that I kept declining to fix in my Google App Engine application. I wanted to upload a file and parse some text from that file with Python. I specifically wanted to bypass the blobstore (for reasons that don't matter here).
Super simple file upload.
All the posts I found said: don't do this, use blobstore. Instead of just answering the posters question. They would then post a helpful example with blobstore use but only a snippet. I already know I should use blobstore though and my blobstore example works. This was similar but didn't work and I couldn't figure out why.. When I requested the file from self.request.get('filename') I simply got back the file name I was uploading and no file.
My problem: def get(self): instead of def post(self); for the function declaration. DOH!
The entire simple example is below so hopefully someone can find the real answer to what they ask instead of "don't do what you asked, do x"
def post(self):
self.response.out.write("----Attempting to upload: %s----\n"%self.request.get('filetype')) #Some other attribute I wanted
stringReader = csv.reader(StringIO.StringIO(self.request.get('file')))
for row in stringReader:
self.response.out.write('Row:%s'%row[0])
The form should look like this:
<form method="POST" enctype="multipart/form-data" size="500" action="/api/save/url">
<select name="filetype">
<option value="music">Music</option>
</select>
File:
<input type="file" name="file"/>
<input type="submit" value="Upload"/><