blobstore中的非ascii文件名(Google App Engine)

时间:2015-02-07 17:11:06

标签: python google-app-engine blobstore

我正在尝试使用Blobstore将一些图片上传到Google App Engine。 并且一些文件包含非ascii字符。 当我下载这些文件时,这些下载文件的文件名似乎在blobstore中显示“ key ”,而不是原始文件名

我的网站是http://wlhunaglearn.appspot.com/

我已在save_as=blob_info.filename中添加BlobstoreDownloadHandler,但在文件名包含非ascii字符时失败。

有什么建议吗? 提前谢谢。

以下是我的main.py文件

# -*- encoding: utf-8 -*-
import os
import urllib
import webapp2

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers


class MainHandler(webapp2.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload')
        self.response.out.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><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" multiple 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, save_as=blob_info.filename)


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

1 个答案:

答案 0 :(得分:4)

搜索所有帖子后,我终于得到了an Answer to another Question

的提示

我弄清楚显示非ascii文件名的正确方法是

urllib.quote函数添加到 ServeHandler class

的最后一行

因此 ServeHandler class 将是:

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, save_as=urllib.quote(blob_info.filename.encode('utf-8')))