Opened 12 years ago

Closed 6 years ago

Last modified 5 years ago

#16470 closed New feature (fixed)

Make FileResponse set the Content-Disposition header

Reported by: mnot@… Owned by: nobody
Component: HTTP handling Version: dev
Severity: Normal Keywords:
Cc: jdunck@…, jreschke Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


RFC6266, recently published by the IETF HTTPbis WG, describes a way to use the Content-Disposition header in HTTP in a manner whereby almost all current browsers can handle files (e.g., downloading with disposition 'attachment') that have non-ASCII characters in them, with other browsers using an ASCII fallback.

Django currently does not have an API for setting Content-Disposition, although there are a few references to it in the documentation. Adding such an API would allow Django sites to download files in any language easily.

Additionally, there's advice in the RFC that, properly implemented in a Django API, would help sites avoid common pitfalls.

For more information, see:

Change History (13)

comment:1 Changed 12 years ago by jreschke

Cc: jreschke added

comment:2 Changed 12 years ago by Aymeric Augustin

Component: InternationalizationHTTP handling
Triage Stage: UnreviewedAccepted

This is a very common problem for developers, and I think it's in the realm of what Django should do. I guess it means adding a method to HttpResponse.

If the implementation isn't completely obvious, please start a discussion to django-developers to ensure consensus.

comment:3 Changed 12 years ago by anonymous

Mark had discussed this with me over twitter/email a couple days ago. He's got an example implementation here:

I was thinking of trying to keep the existing API of dict key/value as header/value pair, but having __setitem__ do some special-casing on Content-Disposition. This would allow do something sensible for all existing code, and we could still add an extra method if people wanted finder control over the fallback filename.

comment:4 Changed 12 years ago by Alex Ogier

This seems to be special case of the encoding in

Seems to me that whatever solution we come up with should generalize to any header that allows RFC 5987-style encodings.

comment:5 Changed 8 years ago by Collin Anderson

Summary: RFC6266 SupportRFC5987/RFC6266 Support (Content-Disposition headers)

comment:6 Changed 6 years ago by Claude Paroz

Now that we have a FileResponse subclass, what about adding a parameter to this subclass which would then set the Content-Disposition header appropriately? Would that satisfy most use cases?

The PDF output example could be then rewritten as:

import io
from reportlab.pdfgen import canvas
from django.http import FileResponse

def some_view(request):
    # Create a file-like buffer to receive PDF data
    buffer = io.BytesIO()

    # Create the PDF object, using the buffer as its "file."
    p = canvas.Canvas(buffer)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")

    # Close the PDF object cleanly, and we're done.
    return FileResponse(
        buffer, as_attachment=True, filename="somefilename.pdf", content_type='application/pdf'
Last edited 6 years ago by Claude Paroz (previous) (diff)

comment:7 Changed 6 years ago by Simon Charette

Version: 1.3master

Claudep I think that would make sense.

comment:8 Changed 6 years ago by Claude Paroz

comment:9 Changed 6 years ago by Claude Paroz

Has patch: set

Made a PR with my commit.

comment:10 Changed 6 years ago by Tim Graham

Summary: RFC5987/RFC6266 Support (Content-Disposition headers)Make FileResponse set the Content-Disposition header
Triage Stage: AcceptedReady for checkin

comment:11 Changed 6 years ago by GitHub <noreply@…>

Resolution: fixed
Status: newclosed

In a177f854:

Fixed #16470 -- Allowed FileResponse to auto-set some Content headers.

Thanks Simon Charette, Jon Dufresne, and Tim Graham for the reviews.

comment:12 Changed 5 years ago by Claude Paroz <claude@…>

In ee52044a:

Refs #16470 -- Fixed typo in a FileResponse test.

comment:13 Changed 5 years ago by Claude Paroz <claude@…>

In 231c595:

[2.1.x] Refs #16470 -- Fixed typo in a FileResponse test.

Backport of ee52044a278885bd9455dd59b1e16c5d5e2d68ce from master.

Note: See TracTickets for help on using tickets.
Back to Top