Method: flask.helpers.send_file
Calls: 4, Exceptions: 0, Paths: 1Back
Path 1: 4 calls (1.0)
'static/index.html' (4)
None (4)
False (4)
None (4)
True (4)
True (4)
None (4)
None (4)
Response (4)
1def send_file(
2 path_or_file: os.PathLike | str | t.BinaryIO,
3 mimetype: str | None = None,
4 as_attachment: bool = False,
5 download_name: str | None = None,
6 conditional: bool = True,
7 etag: bool | str = True,
8 last_modified: datetime | int | float | None = None,
9 max_age: None | (int | t.Callable[[str | None], int | None]) = None,
10) -> Response:
11 """Send the contents of a file to the client.
12
13 The first argument can be a file path or a file-like object. Paths
14 are preferred in most cases because Werkzeug can manage the file and
15 get extra information from the path. Passing a file-like object
16 requires that the file is opened in binary mode, and is mostly
17 useful when building a file in memory with :class:`io.BytesIO`.
18
19 Never pass file paths provided by a user. The path is assumed to be
20 trusted, so a user could craft a path to access a file you didn't
21 intend. Use :func:`send_from_directory` to safely serve
22 user-requested paths from within a directory.
23
24 If the WSGI server sets a ``file_wrapper`` in ``environ``, it is
25 used, otherwise Werkzeug's built-in wrapper is used. Alternatively,
26 if the HTTP server supports ``X-Sendfile``, configuring Flask with
27 ``USE_X_SENDFILE = True`` will tell the server to send the given
28 path, which is much more efficient than reading it in Python.
29
30 :param path_or_file: The path to the file to send, relative to the
31 current working directory if a relative path is given.
32 Alternatively, a file-like object opened in binary mode. Make
33 sure the file pointer is seeked to the start of the data.
34 :param mimetype: The MIME type to send for the file. If not
35 provided, it will try to detect it from the file name.
36 :param as_attachment: Indicate to a browser that it should offer to
37 save the file instead of displaying it.
38 :param download_name: The default name browsers will use when saving
39 the file. Defaults to the passed file name.
40 :param conditional: Enable conditional and range responses based on
41 request headers. Requires passing a file path and ``environ``.
42 :param etag: Calculate an ETag for the file, which requires passing
43 a file path. Can also be a string to use instead.
44 :param last_modified: The last modified time to send for the file,
45 in seconds. If not provided, it will try to detect it from the
46 file path.
47 :param max_age: How long the client should cache the file, in
48 seconds. If set, ``Cache-Control`` will be ``public``, otherwise
49 it will be ``no-cache`` to prefer conditional caching.
50
51 .. versionchanged:: 2.0
52 ``download_name`` replaces the ``attachment_filename``
53 parameter. If ``as_attachment=False``, it is passed with
54 ``Content-Disposition: inline`` instead.
55
56 .. versionchanged:: 2.0
57 ``max_age`` replaces the ``cache_timeout`` parameter.
58 ``conditional`` is enabled and ``max_age`` is not set by
59 default.
60
61 .. versionchanged:: 2.0
62 ``etag`` replaces the ``add_etags`` parameter. It can be a
63 string to use instead of generating one.
64
65 .. versionchanged:: 2.0
66 Passing a file-like object that inherits from
67 :class:`~io.TextIOBase` will raise a :exc:`ValueError` rather
68 than sending an empty file.
69
70 .. versionadded:: 2.0
71 Moved the implementation to Werkzeug. This is now a wrapper to
72 pass some Flask-specific arguments.
73
74 .. versionchanged:: 1.1
75 ``filename`` may be a :class:`~os.PathLike` object.
76
77 .. versionchanged:: 1.1
78 Passing a :class:`~io.BytesIO` object supports range requests.
79
80 .. versionchanged:: 1.0.3
81 Filenames are encoded with ASCII instead of Latin-1 for broader
82 compatibility with WSGI servers.
83
84 .. versionchanged:: 1.0
85 UTF-8 filenames as specified in :rfc:`2231` are supported.
86
87 .. versionchanged:: 0.12
88 The filename is no longer automatically inferred from file
89 objects. If you want to use automatic MIME and etag support,
90 pass a filename via ``filename_or_fp`` or
91 ``attachment_filename``.
92
93 .. versionchanged:: 0.12
94 ``attachment_filename`` is preferred over ``filename`` for MIME
95 detection.
96
97 .. versionchanged:: 0.9
98 ``cache_timeout`` defaults to
99 :meth:`Flask.get_send_file_max_age`.
100
101 .. versionchanged:: 0.7
102 MIME guessing and etag support for file-like objects was
103 deprecated because it was unreliable. Pass a filename if you are
104 able to, otherwise attach an etag yourself.
105
106 .. versionchanged:: 0.5
107 The ``add_etags``, ``cache_timeout`` and ``conditional``
108 parameters were added. The default behavior is to add etags.
109
110 .. versionadded:: 0.2
111 """
112 return werkzeug.utils.send_file( # type: ignore[return-value]
113 **_prepare_send_file_kwargs(
114 path_or_file=path_or_file,
115 environ=request.environ,
116 mimetype=mimetype,
117 as_attachment=as_attachment,
118 download_name=download_name,
119 conditional=conditional,
120 etag=etag,
121 last_modified=last_modified,
122 max_age=max_age,
123 )
124 )