【发布时间】:2014-09-24 22:03:30
【问题描述】:
我是 Python 和 AppEngine 的新手,所以也许我的问题很基础,但我已经搜索了好几个小时......
所以,我正在使用带有 python 和 HTML 的 Google AppEngine...
所以在我的 htlm 文件中我有类似的东西:
<form action="/sign?guestbook_name={{ guestbook_name }}" method="post">
<div><input type="file" name="file"></div>
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
在我的 py 文件中,类似这样的内容:
类留言簿(webapp2.RequestHandler):
def post(self):
# We set the same parent key on the 'Greeting' to ensure each Greeting
# is in the same entity group. Queries across the single entity group
# will be consistent. However, the write rate to a single entity group
# should be limited to ~1/second.
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get('file')
greeting.put()
query_params = {'guestbook_name': guestbook_name}
self.redirect('/?' + urllib.urlencode(query_params))
application = webapp2.WSGIApplication([
('/', MainPage),
('/sign', Guestbook),
], debug=True)
所以我保存了“greeting.content = self.request.get('file')”行,即数据存储区中文件的名称。
但实际上我想上传我的文件。感谢 python 打开并阅读内容,以便上传它的用户可以在他的浏览器中看到文件的内容。
我该怎么做?
我尝试使用:
Import cgi
form = cgi.FieldStorage()
# A nested FieldStorage instance holds the file
fileitem = form['file']
但我有一个“文件”的密钥错误。
那么,我怎样才能读取用户直接在浏览器中上传的文件的内容呢?
【问题讨论】:
-
这里有同样的问题,请查看我的答案:stackoverflow.com/a/30969796/2310396
标签: python html google-app-engine upload