使用GAE“helloworld”示例应用程序作为基础,但将“helloworld.py”更改为:对于<input type =“file”...> self.request.POST [name]只是一个字符串
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.write(
"""
<form method="post">
UPLOAD: <input type="file" name="file_param"/>
<br/>
<input type="submit" name="submit_param" value="Submit">
</form>
"""
)
def post(self):
field_storage = self.request.POST["file_param"]
try:
mimetype = field_storage.type
self.response.write("Mimetype: {}".format(mimetype))
except:
self.response.write("No FieldStorage object, field_storage={}".format(field_storage))
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
运行此谷歌浏览器在Mac OSX:
- 点击 “选择文件”
- 选择要上传的文件
- 点击 “提交”
该回来的网页显示:
No FieldStorage object, field_storage=<the name of the file I uploaded>
根据不同岗位http://webapp-improved.appspot.com/guide/request.html#files和实例,self.request.POST[name]
应该是cgi.FieldStorage对象。但是,如本例所示,self.request.POST[name]
是一个包含上传文件的文件名的字符串。如果它是一个cgi.FieldStorage对象,我希望程序显示上传文件的MIME类型。
我需要cgi.FieldStorage对象,这样我就可以获得mimetype,当然还有值,也就是文件的内容。我究竟做错了什么?
顺便说一下,从webapp2切换到webapp(使用from google.appengine.ext import webapp
)没有什么区别。另外,在Safari或Firefox中运行它,其表现与Chrome中相同。
谢谢!我的示例适用于该更改。 Q1。在使用POST时,您的想法是什么?因为它使用小写字母? Q2。为什么使用Blobstore优先于数据存储BlobProperty(我不明白从文档中)? – Lindsay 2013-03-24 12:33:36
更新了我的答案 – 2013-03-24 15:09:24