编程语言
首页 > 编程语言> > python – 在Google AppEngine中检索.txt文件内容

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

作者:互联网

我正在尝试上传文本文件:

<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 

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

解决方法:

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

标签:python,text,google-app-engine,blobstore
来源: https://codeday.me/bug/20190625/1284687.html