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
web.send_file(path) send_file(path)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| path | String/Fs | Yes | None | The 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
| Type | Description |
|---|---|
| Bool | Returns true if set successfully; the file is sent by the web layer at the end of the request. |
Example
web.header('Cache-Control', 'public, max-age=3600') web.send_file('@/downloads/report.csv')
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_ALLOWpermission 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 byprint. -
Content-Typeis inferred by file extension by default; you can callweb.header('Content-Type', value)in advance when overwriting is required.