【问题标题】:printing files in Google App Engine在 Google App Engine 中打印文件
【发布时间】:2013-05-18 00:03:02
【问题描述】:

我正在尝试在 Google App Engine 中读取和打印文件,但下面的代码似乎没有响应。我可以上传文件,我的期望是它只会打印文本,但它什么也不做。我想过添加一个提交按钮,但我不知道如何将提交与 python 打印联系起来。我怎样才能让它在命令上打印?

我看过 GAE here 提供的示例,但我首先想把它全部放在一个页面上,其次我仍然不明白提交如何调用第二个页面。

import webapp2
from google.appengine.ext.webapp import util

class MainPage(webapp2.RequestHandler):
    #http://bukhantsov.org/2011/12/python-google-app-engine-calculator/
    def get(self):
        # build a list of operations
        self.response.out.write("""<html>
            <body>
            <form action='/' method='get' autocomplete='off'> 
            <input type='file' name='file'/><br/>
            #<input type='submit' name="test" value="submit">
            </form>
            </body>
            </html>""")
        file = self.request.get('file')
        self.response.out.write(file)
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

def main():
    util.run_wsgi_app(app)

if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python html google-app-engine


    【解决方案1】:

    您的表单是使用 HTTP GET 方法发送的,但对于文件上传,您需要 POST。改成:

    method='get'
    

    到:

    method='post'
    

    您还需要以不同的方法处理 POST 请求。 POST 正文本身应以self.request.POST 的形式提供。所以你最终会得到类似的东西:

    def post(self):
        file = self.request.POST['file']
        self.response.out.write(file)
    

    【讨论】:

    • 只是这个更改给了我“405 Method Not Allowed The method POST is not allowed for this resource.”所以我把 file = 和 self.responce... 放在 def post 中,这几乎可以工作,但它打印出文件名而不是文件本身,这是 python 问题还是 html 问题?
    • 抱歉,我忘记了这些内容。更新了答案。
    • 如果您不介意我再打扰您一次,您知道是否有办法将文件打印在与 get 相同的页面上吗?我能找到的所有例子都擦掉页面然后写下文字。
    • 典型的方法是将文件存储在某处,然后重定向到带有显示它的常规 GET 请求的页面。对于简单的情况,您可以在 post() 方法中重复使用相同的 HTML sn-p 并将文件粘贴到某处。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多