Jesvin Jose
Jesvin Jose

Reputation: 23088

How to safely encode the content disposition HTTP header with Python?

response['Content-Disposition'] = 'attachment; filename=%s' % filename

In this Django response, the header causes the intended download but commas in the filename makes Chromium throw a low-level error and spaces cause the filename to change on download. How do I encode filename so the downloaded file gets the same filename?

Converting filename to slug seems to solve the issue, but I want to download with the original filename.

Upvotes: 2

Views: 372

Answers (1)

Julian Reschke
Julian Reschke

Reputation: 42017

Google "IANA message header field registry", find https://www.iana.org/assignments/message-headers/message-headers.xhtml.

Entry for "Content-Disposition" links to: https://www.rfc-editor.org/rfc/rfc6266

Field value can be token or quoted-string, you want quoted-string (which in turn is defined in RFC 2616 (updated by RFC 7230), which will explain how to escape the double quotes).

Note that if you need non-ASCII characters you will need to use "filename*" instead of "filename", as explained in RFC 6266.

Upvotes: 2

Related Questions