2
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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How do I do RFC2616 and RFC 6266 encoding in Python? The best I found is RFC2231 from 2009. djangosnippets.org/snippets/1710

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.