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

标签 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 

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

最佳答案

以下似乎有效,所以肯定还有其他事情正在发生(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 - 在 Google AppEngine 中检索 .txt 文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16186583/

相关文章:

python - “python setup.py notests”报告包的 __init__.py 未被测试覆盖

Java Swing JScrollPane 行号故障

javascript - Photoshop Javascript - 尝试创建新文本图层时出现语法错误

python - 如何在python中转置列表

python - 构建包含多个 Cython 扩展的 Python 包

python - 如何使用存储在单独模块中的错误代码

python - 从 Google App Engine 生态系统迁移

google-app-engine - 将Dart应用程序部署到应用程序引擎时出错

google-app-engine - Google App Engine 和 django-nonrel

javascript - 如何让用户将 html 输入文本区域?如果他们尝试将 &lt;textarea&gt; 包含在文本区域内,我会遇到问题