在Google App Engine中打印文件 [英] printing files in Google App Engine

查看:120
本文介绍了在Google App Engine中打印文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Google App Engine中读取并打印文件,但下面的代码似乎没有响应。我可以上传文件,我的期望是它只会打印文本,但它什么也不做。我想添加一个提交按钮,但我不知道如何链接提交与pythons打印。我怎么才能得到这个命令打印?

I am trying to read and print a file in Google App Engine, but the code bellow seems unresponsive. I can upload the file, and my expectation was that it would just print the text, but it does nothing. I thought about adding a submit button, but I have no idea how to link submit with pythons printing. How can I get this to print on command?

我已经看到GAE 这里,但我首先想把它全部放在一页上,其次我仍然不明白提交如何调用第二页。 / p>

I have seen the example provided by GAE here, but I would first like to keep it all on one page, and second I still don't understand how the submit calls that second page.

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


推荐答案

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

Your form is sent using the HTTP GET method, but for file uploads you need POST. Change it from:

method='get'

到:

method='post'

您还需要使用其他方法处理POST请求。 POST主体本身应该可以用作 self.request.POST 。所以你最终会得到类似的结果:

You will also need to handle POST requests in a different method. The POST body itself should be available as self.request.POST. So you end up with something like:

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

这篇关于在Google App Engine中打印文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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