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]
Xiao Hanyu
source share