'Unicode in Content-Disposition in Post-IE11 area

Accoding to this old answer you need to encode a unicode filename like this:

from urllib.parse import quote

disposition = 'attachment' if as_attachment else 'inline'
try:
    filename.encode('ascii')
    file_expr = 'filename="{}"'.format(filename)
except UnicodeEncodeError:
    file_expr = "filename*=utf-8''{}".format(quote(filename))
response.headers['Content-Disposition'] = '{}; {}'.format(disposition, file_expr)

Just for fun I tried this:

    response = HttpResponse('abcd')
    response['Content-Disposition'] = b'attachment; filename="{}"'.format('üöä & €.txt'.encode('utf8'))
    return response

And Chrome, Firefox and Epiphany (WebKit) where able to download the file to üöä & €.txt.

Which (not outdated) http clients have trouble with this simple utf-8 encoded filename?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source