1

I have a simple web app where the user can submit a form, and include a file to upload if they want, hosted on GAE. I've seen lots of tutorials on how to upload a file to GAE with the blobstore, and they look straightforward, like Upload files in Google App Engine

However, how do I also upload standard form data? I have a bunch of textboxes, and I want the user to be able to submit the attachment as well as the inputted text data. I have only been able to find examples that just upload a file.

Here's my HTML form:

<form action="http://xxx.appspot.com" target="myiframe" method="post" id="myForm" name="myForm">Contact Name<br />
<input type="text" required="" name="cname" /><br />
Name of Institution<br />
<input type="text" required="" name="iname" /><br />
E-Mail<br />
<input type="email" required="" name="email" /><br />
Phone<br />
<input type="tel" required="" name="phone" /><br />
If you have a supporting file that will clarify your help request you can add it here (optional)<br />
<input type="file" name="upfile" MAXLENGTH=50 ALLOW="text/html/text/plain" /><br />
Description of problem/issue<br />
<textarea required="" name="desc" rows="3" cols="30">
</textarea>
<br />
<input type="submit" value="Submit" />
<div id="result"></div>
</form>
<iframe name="myiframe" style="visibility:hidden;display:none" src="http://xxx.appspot.com" id="myiframe"></iframe>

Here's my server side python code. I'm trying to have the user upload a file, then have an email sent that includes both the attached file and the user inputted data:

import cgi, cgitb
from google.appengine.api import mail

class MainPage(webapp2.RequestHandler):

    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, webapp2 World!')

    def post(self):

        contact=self.request.POST["cname"]
        institute=self.request.POST["iname"]
        email=self.request.POST["email"]
        phone=self.request.POST["phone"]
        desc=self.request.POST["desc"]
        filename=self.request.POST["upfile"]
        user_address = "xxx@xxx.net"

        sender_address = "xxx@gmail.com"
        subject = "Test email"

        body = "Contact Name: "+contact+"\n"+"Name of Institution: "+institute+"\n"+"E-mail: "+email+"\n"+"Phone: "+phone+"\n"+"Description: "+desc#+"\n"+"Filename: "+filename
        mail.send_mail(sender_address, user_address, subject, body)

application = webapp2.WSGIApplication([('/', MainPage)], debug=True)
Community
  • 1
  • 1
Amanda_Panda
  • 1,156
  • 4
  • 26
  • 68
  • And what happens when you use that code? – Daniel Roseman Jun 19 '13 at 21:50
  • When I uncomment out the variable filename, I get the actual filename. But is that just the name? How would I attach that to the email? I don't seem to be doing this quite the same way as the GAE docs on email attachments (but then again, they didn't include the relevant html either). – Amanda_Panda Jun 19 '13 at 22:52

1 Answers1

0

Don't forget the enctype in your form:

enctype="multipart/form-data"
Brent Washburne
  • 12,904
  • 4
  • 60
  • 82