Skip to main content

send_file

Send the contents of a file to the client.

def send_file(
path_or_file: os.PathLike[t.AnyStr] | str | t.IO[bytes],
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

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.

Parameters

NameTypeDescription
path_or_file`os.PathLike[t.AnyStr]str
mimetype`strNone` = None
as_attachmentbool = FalseIndicate to a browser that it should offer to save the file instead of displaying it.
download_name`strNone` = None
conditionalbool = TrueEnable conditional and range responses based on request headers. Requires passing a file path and environ.
etag`boolstr` = True
last_modified`datetimeint
max_age`None(int

Returns

TypeDescription
Response