blobstore 中的非 ascii 文件名(Google App Engine)
Non ascii filename in blobstore (Google App Engine)
我正在尝试使用 Blobstore 将一些图片上传到 Google App Engine。
并且一些文件包含非 ascii 字符。
当我下载这些文件时,这些下载文件的 filename 似乎在 blobstore 中显示“key”, 而不是原文件名.
我的网站是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)
搜索了所有的帖子,终于得到了an Answer to another Question
的提示
我发现显示非 ascii 文件名的正确方法是
在ServeHandler class
的最后一行添加一个urllib.quote
函数
因此 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')))
我正在尝试使用 Blobstore 将一些图片上传到 Google App Engine。 并且一些文件包含非 ascii 字符。 当我下载这些文件时,这些下载文件的 filename 似乎在 blobstore 中显示“key”, 而不是原文件名.
我的网站是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)
搜索了所有的帖子,终于得到了an Answer to another Question
的提示我发现显示非 ascii 文件名的正确方法是
在ServeHandler class
的最后一行添加一个urllib.quote
函数
因此 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')))