简体   繁体   English

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

[英]Non ascii filename in blobstore (Google App Engine)

I am trying to upload some picture to Google App Engine using the Blobstore. 我正在尝试使用Blobstore将一些图片上传到Google App Engine。 And some of the files contain non-ascii characters. 并且一些文件包含非ascii字符。 When I download these files, the filename for these downloaded file appeared to show the " key " in blobstore, rather than the original file name . 当我下载这些文件,这些下载的文件的文件名出现,以显示Blobstore的“ 钥匙 ”, 而不是原来的文件名

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

I already add a save_as=blob_info.filename in my BlobstoreDownloadHandler , but it failed when the file name contains non-ascii characters. 我已经在我的BlobstoreDownloadHandler添加了save_as=blob_info.filename ,但是当文件名包含非ascii字符时它失败了。

Any suggestions? 有什么建议? Thanks in advance. 提前致谢。

The following is my main.py file 以下是我的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)

Searching all the posts, I finally got a hint from an Answer to another Question 在搜索所有帖子后,我终于从另一个问题的答案中得到了提示

I figure out the correct way to display non-ascii file name is to 我弄清楚显示非ascii文件名的正确方法是

add a urllib.quote function to the last line of ServeHandler class urllib.quote函数添加到ServeHandler class的最后一行


Therefore the ServeHandler class will be: 因此, 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')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM