Opened 13 years ago

Closed 6 years ago

Last modified 6 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

Description

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:

http://trac.tools.ietf.org/wg/httpbis/trac/wiki/ContentDispositionProducerAdvice

Change History (13)

comment:1 by jreschke, 13 years ago

Cc: jreschke added

comment:2 by Aymeric Augustin, 13 years ago

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 by anonymous, 13 years ago

Mark had discussed this with me over twitter/email a couple days ago. He's got an example implementation here: https://github.com/mnot/sweet/blob/master/lib/index.js

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 by Alex Ogier, 13 years ago

This seems to be special case of the encoding in http://tools.ietf.org/html/rfc5987

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

comment:5 by Collin Anderson, 9 years ago

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

comment:6 by Claude Paroz, 7 years ago

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.
    p.showPage()
    p.save()
    return FileResponse(
        buffer, as_attachment=True, filename="somefilename.pdf", content_type='application/pdf'
    )
Last edited 7 years ago by Claude Paroz (previous) (diff)

comment:7 by Simon Charette, 7 years ago

Version: 1.3master

Claudep I think that would make sense.

comment:8 by Claude Paroz, 7 years ago

comment:9 by Claude Paroz, 7 years ago

Has patch: set

Made a PR with my commit.

comment:10 by Tim Graham, 6 years ago

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

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

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 by Claude Paroz <claude@…>, 6 years ago

In ee52044a:

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

comment:13 by Claude Paroz <claude@…>, 6 years ago

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