Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les fondamentaux
Voir le programme détaillé
Module « flask »

Fonction send_file - module flask

Signature de la fonction send_file

def send_file(path_or_file: 'os.PathLike[t.AnyStr] | str | t.BinaryIO', 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) -> 'Response' 

Description

help(flask.send_file)

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 binary mode, and is mostly
useful when building a file in memory with :class:`io.BytesIO`.

Never pass file paths provided by a user. The path is assumed to be
trusted, so a user could craft a path to access a file you didn't
intend. Use :func:`send_from_directory` to safely serve
user-requested paths from within a directory.

If the WSGI server sets a ``file_wrapper`` in ``environ``, it is
used, otherwise Werkzeug's built-in wrapper is used. Alternatively,
if the HTTP server supports ``X-Sendfile``, configuring Flask with
``USE_X_SENDFILE = True`` will tell the server to send the given
path, which is much more efficient than reading it in Python.

:param path_or_file: The path to the file to send, relative to the
    current working directory if a relative path is given.
    Alternatively, a file-like object opened in binary mode. Make
    sure the file pointer is seeked to the start of the data.
:param mimetype: The MIME type to send for the file. If not
    provided, it will try to detect it from the file name.
:param as_attachment: Indicate to a browser that it should offer to
    save the file instead of displaying it.
:param download_name: The default name browsers will use when saving
    the file. Defaults to the passed file name.
:param conditional: Enable conditional and range responses based on
    request headers. Requires passing a file path and ``environ``.
:param etag: Calculate an ETag for the file, which requires passing
    a file path. Can also be a string to use instead.
:param last_modified: The last modified time to send for the file,
    in seconds. If not provided, it will try to detect it from the
    file path.
:param max_age: How long the client should cache the file, in
    seconds. If set, ``Cache-Control`` will be ``public``, otherwise
    it will be ``no-cache`` to prefer conditional caching.

.. versionchanged:: 2.0
    ``download_name`` replaces the ``attachment_filename``
    parameter. If ``as_attachment=False``, it is passed with
    ``Content-Disposition: inline`` instead.

.. versionchanged:: 2.0
    ``max_age`` replaces the ``cache_timeout`` parameter.
    ``conditional`` is enabled and ``max_age`` is not set by
    default.

.. versionchanged:: 2.0
    ``etag`` replaces the ``add_etags`` parameter. It can be a
    string to use instead of generating one.

.. versionchanged:: 2.0
    Passing a file-like object that inherits from
    :class:`~io.TextIOBase` will raise a :exc:`ValueError` rather
    than sending an empty file.

.. versionadded:: 2.0
    Moved the implementation to Werkzeug. This is now a wrapper to
    pass some Flask-specific arguments.

.. versionchanged:: 1.1
    ``filename`` may be a :class:`~os.PathLike` object.

.. versionchanged:: 1.1
    Passing a :class:`~io.BytesIO` object supports range requests.

.. versionchanged:: 1.0.3
    Filenames are encoded with ASCII instead of Latin-1 for broader
    compatibility with WSGI servers.

.. versionchanged:: 1.0
    UTF-8 filenames as specified in :rfc:`2231` are supported.

.. versionchanged:: 0.12
    The filename is no longer automatically inferred from file
    objects. If you want to use automatic MIME and etag support,
    pass a filename via ``filename_or_fp`` or
    ``attachment_filename``.

.. versionchanged:: 0.12
    ``attachment_filename`` is preferred over ``filename`` for MIME
    detection.

.. versionchanged:: 0.9
    ``cache_timeout`` defaults to
    :meth:`Flask.get_send_file_max_age`.

.. versionchanged:: 0.7
    MIME guessing and etag support for file-like objects was
    removed because it was unreliable. Pass a filename if you are
    able to, otherwise attach an etag yourself.

.. versionchanged:: 0.5
    The ``add_etags``, ``cache_timeout`` and ``conditional``
    parameters were added. The default behavior is to add etags.

.. versionadded:: 0.2


Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les compléments
Voir le programme détaillé