From How to encode the filename parameter of Content-Disposition header in HTTP? I learnt that the encoding defined in RFC 5987 is used to encode filenames in Content-disposition
headers. And from https://stackoverflow.com/a/1361646/739619 I learnt that support in major browsers is good at least since November 2012. Both questions are rather old, yet I couldn't find a standard way to encode filenames according to this encoding in python / tornado. I have a
self.set_header('Content-Disposition', 'attachment;filename="{}.{}"'.format(basename, format))
in my code that fails when basename
contains characters outside latin1, and I am loking for a standard way to encode it.
答案 0 :(得分:2)
您可以使用urllib.parse.quote
进行编码。只需添加filename*=UTF-8''
的样板。例如,这个简单的服务器提供一个UTF-8文件名的文件:
import tornado.httpserver
import tornado.ioloop
import tornado.web
import urllib.parse
class MainHandler(tornado.web.RequestHandler):
def get(self):
filename = 'file "\'ä↭.txt'
encoded_filename = urllib.parse.quote(filename, encoding='utf-8')
self.set_header(
'Content-Disposition',
'attachment;filename*=UTF-8\'\'{}'.format(encoded_filename))
self.write('text file with file name file "\'ä↭.txt.\n')
self.write('Most browsers will encode the " as _ or so.')
application = tornado.web.Application([
(r"/", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.current().start()