Google AppEngine Python Web上传文件并阅读内容 [英] Google AppEngine Python Web upload file and read the content

查看:98
本文介绍了Google AppEngine Python Web上传文件并阅读内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和AppEngine的新手,所以也许我的问题是基本的,但我已经搜索了好几个小时......

所以,我正在使用Google AppEngine with python and HTML ...



所以在我的htlm文件中我有这样的内容:

 < form action =/ sign?guestbook_name = {{guestbook_name}}method =post> 
< div>< input type =filename =file>< / div>
< div>< textarea name =contentrows =3cols =60>< / textarea>< / div>
< div>< input type =submitvalue =Sign Guestbook>< / div>
< / form>

在我的py文件中,类似这样的内容:



class Guestbook(webapp2.RequestHandler):

  def post(self):
# 'Greeting'上的相同父键以确保每个Greeting
#位于同一个实体组中。单个实体组
#中的查询将保持一致。但是,对单个实体组
#的写入速率应限制为〜1 /秒。
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent = guestbook_key(guestbook_name))

如果users.get_current_user( ):
greeting.author = users.get_current_user()

greeting.content = self.request.get('file')


问候语.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的内容,以便上传它的用户可以在浏览器中看到文件的内容。



我该怎么做?



我试过使用:

 导入cgi 
form = cgi.FieldStorage()

#嵌套的FieldStorage实例包含文件
fileitem = form ['file']

但是我有'文件'的关键错误。

那么,我如何才能直接在浏览器中读取用户上传文件的内容呢?

解决方案

您可以使用cgi.FieldStorage()上传文件< 32兆字节。但您必须将表单作为 enctype =multipart / form-data

 < form action =/ uploadenctype =multipart / form-datamethod =post> 
< div>< input type =filename =file/>< / div>
< div>< input type =submitvalue =上传>< / div>
< / form>

/ upload webapp2中的post方法请求处理程序:

  def post(self):

field_storage = self.request.POST.get (文件,无)
如果isinstance(field_storage,cgi.FieldStorage):
file_name = field_storage.filename
file_data = field_storage.file.read())
.. ...

其他:
logging.error('上传失败')

取自此要点的示例


I'm new at Python AND AppEngine, so maybe my question is basic but I've searched it for hours...

So, I'm using Google AppEngine with python and HTML...

So in my htlm file I have something like that :

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

and in my py file, something like that :

class Guestbook(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)

So I saved thank's to the line " greeting.content = self.request.get('file')" the name of the file in the datastore.

But in fact I want to upload my file. Open and read the content thank's to python so that the user who upload it can see the content of the file in his browser.

How can I do it ?

I tried to use :

Import cgi
form = cgi.FieldStorage()

# A nested FieldStorage instance holds the file
fileitem = form['file']

But I have an Key error with 'file'.

So, how can I read the content of the file a user have uploaded directly in his browser ?

解决方案

You can use cgi.FieldStorage() to upload a file < 32 megabytes. But you have to send the form as enctype="multipart/form-data"

<form action="/upload" enctype="multipart/form-data" method="post">
    <div><input type="file" name="file"/></div>
    <div><input type="submit" value="Upload"></div>
</form>

The post method in the /upload webapp2 request handler:

def post(self):

    field_storage = self.request.POST.get("file", None)
    if isinstance(field_storage, cgi.FieldStorage):
        file_name = field_storage.filename
        file_data = field_storage.file.read())
        ..... 

    else:
        logging.error('Upload failed')

Example taken from this gist

这篇关于Google AppEngine Python Web上传文件并阅读内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆