Upload files to webapp2 / GAE

I need to download and process a CSV file from a form in a Google App Engine application based on Webapp2 (Python). I understand that I could use blobstore to temporarily store the file, but I'm curious to know if there is a way to process the file without saving it at all.

+3
source share
2 answers

The content of the downloaded files is in self.request.POST in your handler, so you can get this content (suppose, for example, a field for the downloaded file named 'foo' ), for example,

 content = self.request.POST.multi['foo'].file.read() 

So now you have the content as a string - handle it as you wish. This, of course, assumes that there will be in memory (without downloading several megabytes!) ...

+3
source

If you need to upload a file via webapp2 using an HTML form, the first thing you need to do is change the HTML form attribute enctype to multipart/form-data , so the code snippet looks like this:

 <form action="/emails" class="form-horizontal" enctype="multipart/form-data" method="post"> <input multiple id="file" name="attachments" type="file"> </form> 

In python code, you can read the file directly through request.POST , here is an example code fragment:

 class UploadHandler(BaseHandler): def post(self): attachments = self.request.POST.getall('attachments') _attachments = [{'content': f.file.read(), 'filename': f.filename} for f in attachments] 
+3
source

All Articles