GAE (Python) form file upload + email file as atta

2019-08-30 19:09发布

I need to implement a rather simple web form on Google App Engine (GAE-Python) that accepts some form input (name, email, telephone) and a resume (typically TXT, PDF or DOC/DOCX) file. Once the form is submitted, I want to contents of the form to be emailed and if a file is submitted in the form, for it to be included as an attachment in the same email to a designated email address.

  1. I don't need the file to exist after it is emailed/attached. Sort of like a temp file, or stored in a temp blob store.
  2. With HTML5 and jQuery, there are lot of fancy user interfaces to implement file uploads. Is there any recommended approach to use one of these that work nicely with GAE, as well as being able to degrade gracefully if the browser does not support the modern methods (namely IE)?

I am using jinja2 framework, if that is relevant. (I am a Python novice by the way)

Thanks in advance!

1条回答
干净又极端
2楼-- · 2019-08-30 19:40

For upload a file as a blob in GAE you need the blobstore_handlers from built-in framework called webapp. The docs have a complete sample for upload files and I do not think there are other ways to upload to the blobstore.

When you have the blob see the first sample of this page from docs for attach the blob to the email.

Now, for a "temp file solution" you can try a different way: write the upload file to the ram with StringIO python module. Something like that:

<form action="/test/" method="POST" enctype="multipart/form-data">
  <input type="file" name="file"><br>
  <input type="submit"name="submit" value="Submit">
</form>
def post(self):
    output = StringIO.StringIO()
    upload = self.request.get('file')
    output.write(upload)
    self.response.write(output.getvalue())
查看更多
登录 后发表回答