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, 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 | None = None, file_path: str | PathLike | Path, file_system: BaseFileSystem, filename: str = '', headers: dict[str, str] | None = None, is_head_response: bool = False, media_type: MediaType | str | 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.

  • chunk_size – The chunk size to use.

  • content_disposition_type – The type of the Content-Disposition. Either inline or attachment.

  • 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.

  • status_code – The response status code.

async send_body(send: Send, receive: Receive) None

Emit a stream of events correlating with the response body.

Parameters:
  • send – The ASGI send function.

  • receive – The ASGI receive function.

Returns:

None

async start_response(send: Send) None

Emit the start event of the response. This event includes the headers and status codes.

Parameters:

send – The ASGI send function.

Returns:

None

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 | None = None, file_system: str | BaseFileSystem | AbstractFileSystem | AbstractAsyncFileSystem | None = None, filename: str | None = None, headers: ResponseHeaders | None = None, media_type: Literal[MediaType.TEXT] | str | None = None, status_code: int | None = None) None

Send a file from a file system.

Parameters:
  • path – A file path in one of the supported formats.

  • background – A BackgroundTask instance or BackgroundTasks 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. Either inline or attachment.

  • cookies – A list of Cookie instances to be set under the response Set-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

  • file_system – The file system to load the file from. Instances of BaseFileSystem, fsspec.spec.AbstractFileSystem, fsspec.asyn.AsyncFileSystem will be used directly. If passed string, use it to look up the corresponding file system from the FileSystemRegistry. If not given, the file will be loaded from default

  • 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 to application/octet-stream.

  • status_code – An HTTP status code.

to_asgi_response(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:
  • 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 is

  • type_encoders – A dictionary of type encoders to use for encoding the response content.

Returns:

A low-level ASGI file response.

litestar.response.file.create_etag_for_file(path: PathType, modified_time: float | None, file_size: int) str

Create an etag.

Notes

  • Function is derived from flask.

Returns:

An etag.