python – blobstore中的非ascii文件名(Google App Engine)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – blobstore中的非ascii文件名(Google App Engine),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2043字,纯文字阅读大概需要3分钟。
内容图文
![python – blobstore中的非ascii文件名(Google App Engine)](/upload/InfoBanner/zyjiaocheng/795/b9dd259b999d43af8335ab84dab985cf.jpg)
我正在尝试使用Blobstore将一些图片上传到Google App Engine.
并且一些文件包含非ascii字符.
当我下载这些文件时,这些下载文件的文件名似乎在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文件名的正确方法是
将urllib.quote函数添加到ServeHandler类的最后一行
因此,ServeHandler类将是:
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')))
内容总结
以上是互联网集市为您收集整理的python – blobstore中的非ascii文件名(Google App Engine)全部内容,希望文章能够帮你解决python – blobstore中的非ascii文件名(Google App Engine)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。