4

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?

Maoz Zadok
  • 4,871
  • 3
  • 33
  • 43
guettli
  • 25,042
  • 81
  • 346
  • 663
  • 2
    Per [this](https://stackoverflow.com/a/55157306/4935162) answer it seems all current browsers have no problems with it. Also have a look at the RFCs mentioned there. Are you also asking about CLI clients? Or http libraries? – Yarin_007 May 23 '22 at 09:02
  • @Yarin_007 for my current use case the support from current browsers is enough. – guettli May 23 '22 at 09:37

0 Answers0