ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-24 00:43:52  阅读:153  来源: 互联网

标签:python google-app-engine blobstore


我正在尝试使用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,google-app-engine,blobstore
来源: https://codeday.me/bug/20190624/1275623.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有