在Google AppEngine中检索.txt文件内容

时间:2013-04-24 08:00:52

标签: python google-app-engine text blobstore

我正在尝试使用以下方式上传文本文件:

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

并使用以下方法检索此文件:

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

我的输出是:

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 

有什么方法可以检索文件内容而不是上面的标题。 ??

1 个答案:

答案 0 :(得分:4)

以下似乎有效,因此必须有其他事情发生(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)