如何在网络上显示文字,通过gae,谷歌应用引擎,python

时间:2012-12-12 08:51:49

标签: python html google-app-engine

我想上传txt文件并显示此文本。

我有将txt上传到gae的代码,但是如何显示它?

我想上传txt文件并显示此文本。

我有将txt上传到gae的代码,但是如何显示它?

我想上传txt文件并显示此文本。

我有将txt上传到gae的代码,但是如何显示它?

    import os
    import urllib

    from google.appengine.ext import blobstore
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp import blobstore_handlers
    from google.appengine.ext.webapp import template
    from google.appengine.ext.webapp.util import run_wsgi_app


    class MainHandler(webapp.RequestHandler):
        def get(self):
            upload_url = blobstore.create_upload_url('/upload')
            self.response.out.write('<html><body>')
            self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
            self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""")

            for b in blobstore.BlobInfo.all():
                self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>')


   class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
        def post(self):
            upload_files = self.get_uploads('file')
            blob_info = upload_files[0]
            self.redirect('/')


   class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
        def get(self, blob_key):
            blob_key = str(urllib.unquote(blob_key))
            if not blobstore.get(blob_key):
                self.error(404)
            else:
                self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)

        def main():
            application = webapp.WSGIApplication(
                  [('/', MainHandler),
                   ('/upload', UploadHandler),
                   ('/serve/([^/]+)?', ServeHandler),
                  ], debug=True)
            run_wsgi_app(application)


     if __name__ == '__main__':
        main()

3 个答案:

答案 0 :(得分:1)

您应该稍微更改上传处理程序的重定向功能,最后您应该重定向到上传的文件:

self.redirect('/serve/%s/' % blob_info.key())

另外,你的服务员(我不确定你可以使用这个,如果你创建了自己的serve_txt函数可能会更好,我会尝试)

class ServeHandler(webapp.RequestHandler): 
  #we want to render a page, not a download, that's why it extends RequestHandler
  def get(self, resource):
    resource = str(urllib.unquote(resource))
    blob_info = blobstore.BlobInfo.get(resource) #this should be your file
    self.response.headers['Content-Type'] = 'text/html'
    self.response.out.write(blob_info)

模板完全取决于你现在,这应该在你的页面上显示一个纯文本(作为一个txt,你可能希望在使用BlobReader class的python之前阅读它然后返回正确格式化的页面。我的只是关于你应该如何进行的一个例子)

答案 1 :(得分:0)

如果您想在浏览器中显示而不是保存 - 请删除

save_as =特朗普>

   class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
        def get(self, blob_key):
            blob_key = str(urllib.unquote(blob_key))
            if not blobstore.get(blob_key):
                self.error(404)
            else:
                self.send_blob(blobstore.BlobInfo.get(blob_key))

答案 2 :(得分:0)

谢谢---------喜欢它

import os
import urllib

from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload')
        self.response.out.write('<html><body>')
        self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
        self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""")

        for b in blobstore.BlobInfo.all():
            #self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>')
            self.response.out.write(u'<li><a href="/serve/%s">%s</a>[%s]</li>' % (b.key(),b.filename, blobstore.BlobReader(b.key()).read())) 

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        self.redirect('/')
       #contents = open(filename).read()

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
        def get(self, blob_key):
            blob_key = str(urllib.unquote(blob_key))
            if not blobstore.get(blob_key):
                self.error(404)
            else:
                self.send_blob(blobstore.BlobInfo.get(blob_key))

def main():
    application = webapp.WSGIApplication(
          [('/', MainHandler),
           ('/upload', UploadHandler),
           ('/serve/([^/]+)?', ServeHandler),
          ], debug=True)
    run_wsgi_app(application)
    print "Hello, world!1"

if __name__ == '__main__':
  main()