2013-06-19 25 views
1

我有一個簡單的網絡應用程序,用戶可以在其中提交表單幷包含要上傳的文件(如果他們想要,在GAE上託管)。我見過很多關於如何使用blobstore上傳文件到GAE的教程,並且它們看起來很簡單,就像Upload files in Google App Engine使用Google App Engine在Python中上傳文件和表單數據

但是,我怎樣才能上傳標準表單數據呢?我有一堆文本框,我希望用戶能夠提交附件以及輸入的文本數據。我只能找到只上傳文件的例子。

這裏是我的HTML表單:

<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> 

這裏是我的服務器端Python代碼。我試圖讓用戶上傳文件,然後有一個電子郵件發送包含附加的文件和用戶輸入的數據都:

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 = "[email protected]" 

     sender_address = "[email protected]" 
     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) 
+0

當您使用該代碼時會發生什麼? –

+0

當我取消註釋變量文件名時,我得到了實際的文件名。但那是不是名字?我將如何附加到電子郵件?我似乎沒有像電子郵件附件中的GAE文檔那樣執行此操作(但是,他們再次沒有包括相關的html)。 –

回答

0

不要忘記你的形式是enctype:

enctype="multipart/form-data" 
+0

當我包含它,我得到 TypeError:強制轉換爲Unicode:需要字符串或緩衝區,發現實例 在我的GAE日誌中。 –

+0

也許這將有助於:enctype =「multipart/form-data; charset = UTF-8」 –

+0

謝謝,這是有效的!現在我只需要弄清楚如何附加文件... –

相關問題