从GAE blobstore读取文件

时间:2013-12-24 14:40:32

标签: google-app-engine

使用以下代码,

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

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
        blob_info = upload_files[0]
        self.redirect('/serve/%s' % blob_info.key())

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info)

app = webapp.WSGIApplication(
  [('/', MainHandler),
    ('/upload', UploadHandler),
    ('/serve/([^/]+)?', ServeHandler),
  ], debug=True)

if __name__ == '__main__':
    run_wsgi_app(app)

我能够从我的blob商店中读取.pdf,.txt和媒体文件等文件。但是像.doc,.docx这样的文件会返回不可读的文件。 我尝试使用blob_reader但仍然无效,如何读取.doc和.docx等文件?

1 个答案:

答案 0 :(得分:2)

您的浏览器无法处理doc文件。但您可以下载该文件并使用查看器打开该文件。

要下载blob,请使用:

self.send_blob(blob_info,save_as=True)

或:

self.send_blob(blob_info,save_as='amsdoc.docx')