Retrieving .txt file contents in Google AppEngine

2019-05-05 10:53发布

I'm trying to Upload a text file using :

<input type="file" name="file">

and this file is retrieved using:

class UploadHandler(webapp2.RequestHandler):
  def post(self):
    file=self.request.POST['file']
    self.response.headers['Content-Type'] = "text/plain"
    self.response.write(file.value)

my output is:

Content-Type: text/plain MIME-Version: 1.0 Content-Length: 312 Content-MD5: MDIzYzM5YmNmOWRmMzY5Zjk2MTYzZTUzNjYwMTg5YjM= content-type: text/plain content-disposition: form-data; name="file"; filename="blah.txt" X-AppEngine-Upload-Creation: 2013-04-24 07:57:23.729774 

Is there any way i could retrive the file content instead of above headers. ??

1条回答
太酷不给撩
2楼-- · 2019-05-05 11:46

The following seems to work, so there must be something else that is happening (live example):

import webapp2

class MainHandler(webapp2.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = "text/html"
    self.response.write('''
      <form action="/upload" enctype="multipart/form-data" method="post">
        <input type="file" name="file">
        <input type="submit">
      </form>
    ''')

class UploadHandler(webapp2.RequestHandler):
  def post(self):
    file = self.request.POST['file']
    self.response.headers['Content-Type'] = "text/plain"
    self.response.write(file.value)

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/upload', UploadHandler)
  ], debug=True)
查看更多
登录 后发表回答