gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:00:43 24 4
gpt4 key购买 nike

我正在尝试使用 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 文件名的正确方法是

ServeHandler类的最后一行添加一个urllib.quote函数


因此 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),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28384971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com