diff options
| author | Claude Paroz <claude@2xlibre.net> | 2018-05-15 18:12:11 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-05-15 18:12:11 +0200 |
| commit | a177f854c34718e473bcd0a2dc6c4fd935c8e327 (patch) | |
| tree | d15e436c26edfd2037972c48095b4bcd2ad48505 /docs/ref | |
| parent | 2dcc5d629a6439b5547cdd6e67815cabf608fcd4 (diff) | |
Fixed #16470 -- Allowed FileResponse to auto-set some Content headers.
Thanks Simon Charette, Jon Dufresne, and Tim Graham for the reviews.
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/request-response.txt | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt index 0caf37bc99..05cb24f3b1 100644 --- a/docs/ref/request-response.txt +++ b/docs/ref/request-response.txt @@ -1054,17 +1054,45 @@ Attributes ``FileResponse`` objects ======================== -.. class:: FileResponse +.. class:: FileResponse(open_file, as_attachment=False, filename='', **kwargs) -:class:`FileResponse` is a subclass of :class:`StreamingHttpResponse` optimized -for binary files. It uses `wsgi.file_wrapper`_ if provided by the wsgi server, -otherwise it streams the file out in small chunks. + :class:`FileResponse` is a subclass of :class:`StreamingHttpResponse` + optimized for binary files. It uses `wsgi.file_wrapper`_ if provided by the + wsgi server, otherwise it streams the file out in small chunks. + + If ``as_attachment=True``, the ``Content-Disposition`` header is set, which + asks the browser to offer the file to the user as a download. + + If ``open_file`` doesn't have a name or if the name of ``open_file`` isn't + appropriate, provide a custom file name using the ``filename`` parameter. + + The ``Content-Length``, ``Content-Type``, and ``Content-Disposition`` + headers are automatically set when they can be guessed from contents of + ``open_file``. + + .. versionadded:: 2.1 + + The ``as_attachment`` and ``filename`` keywords argument were added. + Also, ``FileResponse`` sets the ``Content`` headers if it can guess + them. .. _wsgi.file_wrapper: https://www.python.org/dev/peps/pep-3333/#optional-platform-specific-file-handling -``FileResponse`` expects a file open in binary mode like so:: +``FileResponse`` accepts any file-like object with binary content, for example +a file open in binary mode like so:: >>> from django.http import FileResponse >>> response = FileResponse(open('myfile.png', 'rb')) The file will be closed automatically, so don't open it with a context manager. + +Methods +------- + +.. method:: FileResponse.set_headers(open_file) + + .. versionadded:: 2.1 + + This method is automatically called during the response initialization and + set various headers (``Content-Length``, ``Content-Type``, and + ``Content-Disposition``) depending on ``open_file``. |
