python – 在Flask响应头中设置Unicode文件名

我正在尝试设置Content-Disposition标头以将文件发送到客户端.文件名是Unicode.当我尝试设置标头时,它失败并出现UnicodeEncodeError.我尝试了编码和解码的各种组合,但无法使其工作.如何发送带有Unicode文件名的文件?

destination_file = 'python_report.html'
response.headers['Content-Disposition'] = 'attachment; filename=' + destination_file
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/server.py", line 495, in send_header
    ("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'strict'))
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 41-42: ordinal not in range(256)

最佳答案
RFC 2231 section 4描述了如何指定要使用的编码而不是Latin-1作为标头值.使用标题选项filename * = UTF-8”…,其中…是url编码的名称.您还可以包含filename选项以提供Latin-1后备.

直到最近,浏览器才一直支持这一点. This page有一些关于浏览器支持的指标.值得注意的是,IE8将忽略UTF-8选项,如果UTF-8选项出现在Latin-1选项之前,它将完全失败.

Flask 1.0 supports calling send_file with Unicode filenames.如果使用Flask 1.0,则可以使用带有as_attachment = True和Unicode文件名的send_file.

from flask import send_file

@app.route('/send-python-report')
def send_python_report():
    return send_file('python_report.html', as_attachment=True)

在此之前,您可以使用Flask将使用的相同过程手动构建标题.

import unicodedata

from flask import send_file
from werkzeug.urls import url_quote

@app.route('/send-python-report')
def send_python_report():
    filename = 'python_report.html'
    rv = send_file(filename)

    try:
        filename = filename.encode('latin-1')
    except UnicodeEncodeError:
        filenames = {
            'filename': unicodedata.normalize('NFKD', filename).encode('latin-1', 'ignore'),
            'filename*': "UTF-8''{}".format(url_quote(filename)),
        }
    else:
        filenames = {'filename': filename}

    rv.headers.set('Content-Disposition', 'attachment', **filenames)
    return rv

为了安全起见,如果文件名由用户输入提供,则应使用send_from_directory.该过程与上述相同,取代了该功能.

WSGI不确保头选项的顺序,因此如果要支持IE8,则必须使用带有OrderedDict的dump_options_header完全手动构造头值.否则,filename *可能会出现在filename之前,如上所述,这在IE8中不起作用.

from collections import OrderedDict
import unicodedata

from flask import send_file
from werkzeug.http import dump_options_header
from werkzeug.urls import url_quote

@app.route('/send-python-report')
def send_python_report():
    filename = 'python_report.html'
    rv = send_file(filename)
    filenames = OrderedDict()

    try:
        filename = filename.encode('latin-1')
    except UnicodeEncodeError:
        filenames['filename'] = unicodedata.normalize('NFKD', filename).encode('latin-1', 'ignore')
        filenames['filename*']: "UTF-8''{}".format(url_quote(filename))
    else:
        filenames['filename'] = filename

    rv.headers.set('Content-Disposition', dump_options_header('attachment', filenames))
    return rv
点赞