我正在尝试使用Blobstore将一些图片上传到Google App Engine。其中一些文件包含非ASCII字符。当我下载这些文件时,下载的文件文件名似乎显示Blobstore中的“key”,而不是原始文件名。
我的网站是http://wlhunaglearn.appspot.com/ 我已经在我的
有什么建议吗?先谢谢了。
以下是我的main.py文件:
我的网站是http://wlhunaglearn.appspot.com/ 我已经在我的
BlobstoreDownloadHandler
中添加了save_as=blob_info.filename
,但当文件名包含非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)
BlobInfo
的filename
是什么类型的“字符串”(我认为是Unicode字符串--您可以通过简单的logging.info
和type(blob_info.filename)
来轻松检查!),以及save_as
参数所需的“字符串”是什么类型(我相信是任意字节的字符串)--而不是在详尽研究之后延迟响应,我发布这篇文章是为了指出这正是整个问题的关键。如果我的想法是正确的,那么save_as=blob_info.filename.encode('utf8')
就能解决所有问题;否则,一些日志记录和报告回溯将帮助我帮助您! - Alex Martelli