file#
- class litestar.response.file.ASGIFileResponse#
Bases:
ASGIStreamingResponse
A low-level ASGI response, streaming a file as response body.
- __init__(*, background: BackgroundTask | BackgroundTasks | None = None, body: bytes | str = b'', chunk_size: int = 1048576, content_disposition_type: Literal['attachment', 'inline'] = 'attachment', content_length: int | None = None, cookies: Iterable[Cookie] | None = None, encoded_headers: Iterable[tuple[bytes, bytes]] | None = None, encoding: str = 'utf-8', etag: ETag | None = None, file_info: FileInfo | Coroutine[None, None, FileInfo] | None = None, file_path: str | PathLike | Path, file_system: FileSystemProtocol | None = None, filename: str = '', headers: dict[str, str] | None = None, is_head_response: bool = False, media_type: MediaType | str | None = None, stat_result: stat_result_type | None = None, status_code: int | None = None) None #
A low-level ASGI response, streaming a file as response body.
- Parameters:
background – A background task or a list of background tasks to be executed after the response is sent.
body – encoded content to send in the response body.
chunk_size – The chunk size to use.
content_disposition_type – The type of the
Content-Disposition
. Eitherinline
orattachment
.content_length – The response content length.
cookies – The response cookies.
encoded_headers – A list of encoded headers.
encoding – The response encoding.
etag – An etag.
file_info – A file info.
file_path – A path to a file.
file_system – A file system adapter.
filename – The name of the file.
headers – A dictionary of headers.
headers – The response headers.
is_head_response – A boolean indicating if the response is a HEAD response.
media_type – The media type of the file.
stat_result – A stat result.
status_code – The response status code.
- class litestar.response.file.File#
Bases:
Response
A response, streaming a file as response body.
- __init__(path: str | PathLike | Path, *, background: BackgroundTask | BackgroundTasks | None = None, chunk_size: int = 1048576, content_disposition_type: Literal['attachment', 'inline'] = 'attachment', cookies: ResponseCookies | None = None, encoding: str = 'utf-8', etag: ETag | None = None, file_info: FileInfo | Coroutine[Any, Any, FileInfo] | None = None, file_system: FileSystemProtocol | None = None, filename: str | None = None, headers: ResponseHeaders | None = None, media_type: Literal[MediaType.TEXT] | str | None = None, stat_result: stat_result_type | None = None, status_code: int | None = None) None #
Initialize
File
Notes
This class extends the
Stream
class.
- Parameters:
path – A file path in one of the supported formats.
background – A
BackgroundTask
instance orBackgroundTasks
to execute after the response is finished. Defaults to None.chunk_size – The chunk sizes to use when streaming the file. Defaults to 1MB.
content_disposition_type – The type of the
Content-Disposition
. Eitherinline
orattachment
.cookies – A list of
Cookie
instances to be set under the responseSet-Cookie
header.encoding – The encoding to be used for the response headers.
etag – An optional
ETag
instance. If not provided, an etag will be generated.file_info – The output of calling
file_system.info
, equivalent to providing anos.stat_result
.file_system – An implementation of the
FileSystemProtocol
. If provided it will be used to load the file.filename – An optional filename to set in the header.
headers – A string keyed dictionary of response headers. Header keys are insensitive.
media_type – A value for the response
Content-Type
header. If not provided, the value will be either derived from the filename if provided and supported by the stdlib, or will default toapplication/octet-stream
.stat_result – An optional result of calling :func:os.stat:. If not provided, this will be done by the response constructor.
status_code – An HTTP status code.
- to_asgi_response(app: Litestar | None, request: Request, *, background: BackgroundTask | BackgroundTasks | None = None, encoded_headers: Iterable[tuple[bytes, bytes]] | None = None, cookies: Iterable[Cookie] | None = None, headers: dict[str, str] | None = None, is_head_response: bool = False, media_type: MediaType | str | None = None, status_code: int | None = None, type_encoders: TypeEncodersMap | None = None) ASGIFileResponse #
Create an
ASGIFileResponse
instance.- Parameters:
app – The
Litestar
application instance.background – Background task(s) to be executed after the response is sent.
cookies – A list of cookies to be set on the response.
encoded_headers – A list of already encoded headers.
headers – Additional headers to be merged with the response headers. Response headers take precedence.
is_head_response – Whether the response is a HEAD response.
media_type – Media type for the response. If
media_type
is already set on the response, this is ignored.request – The
Request
instance.status_code – Status code for the response. If
status_code
is already set on the response, this istype_encoders – A dictionary of type encoders to use for encoding the response content.
- Returns:
A low-level ASGI file response.
- async litestar.response.file.async_file_iterator(file_path: PathType, chunk_size: int, adapter: FileSystemAdapter) AsyncGenerator[bytes, None] #
Return an async that asynchronously reads a file and yields its chunks.
- Parameters:
file_path – A path to a file.
chunk_size – The chunk file to use.
adapter – File system adapter class.
adapter – File system adapter class.
- Returns:
An async generator.