MCPcopy
hub / github.com/pallets/werkzeug / send_file

Function send_file

src/werkzeug/utils.py:312–528  ·  view source on GitHub ↗

Send the contents of a file to the client. The first argument can be a file path or a file-like object. Paths are preferred in most cases because Werkzeug can manage the file and get extra information from the path. Passing a file-like object requires that the file is opened in bina

(
    path_or_file: os.PathLike[str] | str | t.IO[bytes],
    environ: WSGIEnvironment,
    mimetype: str | None = None,
    as_attachment: bool = False,
    download_name: str | None = None,
    conditional: bool = True,
    etag: bool | str = True,
    last_modified: datetime | int | float | None = None,
    max_age: None | (int | t.Callable[[str | None], int | None]) = None,
    use_x_sendfile: bool = False,
    response_class: type[Response] | None = None,
    _root_path: os.PathLike[str] | str | None = None,
)

Source from the content-addressed store, hash-verified

310
311
312def send_file(
313 path_or_file: os.PathLike[str] | str | t.IO[bytes],
314 environ: WSGIEnvironment,
315 mimetype: str | None = None,
316 as_attachment: bool = False,
317 download_name: str | None = None,
318 conditional: bool = True,
319 etag: bool | str = True,
320 last_modified: datetime | int | float | None = None,
321 max_age: None | (int | t.Callable[[str | None], int | None]) = None,
322 use_x_sendfile: bool = False,
323 response_class: type[Response] | None = None,
324 _root_path: os.PathLike[str] | str | None = None,
325) -> Response:
326 """Send the contents of a file to the client.
327
328 The first argument can be a file path or a file-like object. Paths
329 are preferred in most cases because Werkzeug can manage the file and
330 get extra information from the path. Passing a file-like object
331 requires that the file is opened in binary mode, and is mostly
332 useful when building a file in memory with :class:`io.BytesIO`.
333
334 Never pass file paths provided by a user. The path is assumed to be
335 trusted, so a user could craft a path to access a file you didn't
336 intend. Use :func:`send_from_directory` to safely serve user-provided paths.
337
338 If the WSGI server sets a ``file_wrapper`` in ``environ``, it is
339 used, otherwise Werkzeug's built-in wrapper is used. Alternatively,
340 if the HTTP server supports ``X-Sendfile``, ``use_x_sendfile=True``
341 will tell the server to send the given path, which is much more
342 efficient than reading it in Python.
343
344 :param path_or_file: The path to the file to send, relative to the
345 current working directory if a relative path is given.
346 Alternatively, a file-like object opened in binary mode. Make
347 sure the file pointer is seeked to the start of the data.
348 :param environ: The WSGI environ for the current request.
349 :param mimetype: The MIME type to send for the file. If not
350 provided, it will try to detect it from the file name.
351 :param as_attachment: Indicate to a browser that it should offer to
352 save the file instead of displaying it.
353 :param download_name: The default name browsers will use when saving
354 the file. Defaults to the passed file name.
355 :param conditional: Enable conditional and range responses based on
356 request headers. Requires passing a file path and ``environ``.
357 :param etag: Calculate an ETag for the file, which requires passing
358 a file path. Can also be a string to use instead.
359 :param last_modified: The last modified time to send for the file,
360 in seconds. If not provided, it will try to detect it from the
361 file path.
362 :param max_age: How long the client should cache the file, in
363 seconds. If set, ``Cache-Control`` will be ``public``, otherwise
364 it will be ``no-cache`` to prefer conditional caching.
365 :param use_x_sendfile: Set the ``X-Sendfile`` header to let the
366 server to efficiently send the file. Requires support from the
367 HTTP server. Requires passing a file path.
368 :param response_class: Build the response using this class. Defaults
369 to :class:`~werkzeug.wrappers.Response`.

Callers 15

test_pathFunction · 0.90
test_x_sendfileFunction · 0.90
test_last_modifiedFunction · 0.90
test_objectFunction · 0.90
test_text_mode_failsFunction · 0.90
test_disposition_nameFunction · 0.90
test_non_ascii_nameFunction · 0.90
test_max_ageFunction · 0.90

Calls 7

setMethod · 0.95
HeadersClass · 0.85
wrap_fileFunction · 0.85
set_etagMethod · 0.80
make_conditionalMethod · 0.80
closeMethod · 0.45
popMethod · 0.45

Tested by 15

test_pathFunction · 0.72
test_x_sendfileFunction · 0.72
test_last_modifiedFunction · 0.72
test_objectFunction · 0.72
test_text_mode_failsFunction · 0.72
test_disposition_nameFunction · 0.72
test_non_ascii_nameFunction · 0.72
test_max_ageFunction · 0.72