web.send_file

web.send_file

web.send_file

Function

Send local files directly as HTTP responses, suitable for downloading large files or exporting files, to avoid reading the file content into the BT dynamic response string at once.

Syntax

Parameters

ParametersTypeRequiredDefault valueDescription
pathString/FsYesNoneThe local file path to send. The relative path is resolved based on the current source code file directory, and @/ is resolved based on the project root.

Return value

TypeDescription
BoolReturns true if set successfully; the file is sent by the web layer at the end of the request.

Example

Notes

  • Can only be called in a web request.
  • This ability will read local files and is subject to BT_PERMISSION_DENY=fs / BT_PERMISSION_ALLOW permission configuration.
  • File responses are sent in chunks by the web framework, supporting ETag, Last-Modified, conditional requests and HTTP Range.
  • After web.send_file() is set, this request will not write back the last expression or the dynamic response body generated by print.
  • Content-Type is inferred by file extension by default; you can call web.header('Content-Type', value) in advance when overwriting is required.