Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading Files in webapp2/GAE

I need to upload and process a CSV file from a form in a Google App Engine application based on Webapp2 (Python) I understand I could use blobstore to temporary store the file but I am curious to know if there is a way to process the file without having to store it at all.

like image 850
lowcoupling Avatar asked Aug 31 '25 06:08

lowcoupling


1 Answers

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

<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 via request.POST, here's a sample code snippet:

class UploadHandler(BaseHandler):
    def post(self):
        attachments = self.request.POST.getall('attachments')

        _attachments = [{'content': f.file.read(),
                         'filename': f.filename} for f in attachments]
like image 128
Xiao Hanyu Avatar answered Sep 02 '25 18:09

Xiao Hanyu