how to show txt on web, by gae,google app engine,python
Question: i want to upload txt file and show this txt.

i had the code of uploading txt to gae, but how to show it???

i want to upload txt file and show this txt.

i had the code of uploading txt to gae, but how to show it???

i want to upload txt file and show this txt.

i had the code of uploading txt to gae, but how to show it???

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


Answer: you should change the redirect function of your upload handler a bit, in the
end you should redirect to the uploaded file:

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

also, your servehandler (i'm not sure you can use this tho, it may be better
if you created your own serve_txt function, i'll try anyway)

    
    
    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)
    

templating is completely up to you now, this should display a plain text to
your page (being a txt, you may want to read it before with python using
[BlobReader
class](https://developers.google.com/appengine/docs/python/blobstore/blobreaderclass)
then return the page correctly formatted. mine is just an example on how you
should proceed)

