2018-05-22 23:15:08 +02:00
---
title: Static Files
---
2022-07-16 11:59:20 +02:00
import CodeBlock from "@site/src/components/code_block.js";
2018-05-22 23:15:08 +02:00
# Individual file
2022-02-26 05:41:49 +01:00
It is possible to serve static files with a custom path pattern and `NamedFile` . To match a path tail, we can use a `[.*]` regex.
2018-05-22 23:15:08 +02:00
2022-07-16 11:59:20 +02:00
< CodeBlock example = "static-files" file = "main.rs" section = "individual-file" / >
2018-05-22 23:15:08 +02:00
2022-07-16 11:59:20 +02:00
## Directory
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
To serve files from specific directories and sub-directories, `Files` can be used. `Files` must be registered with an `App::service()` method, otherwise it will be unable to serve sub-paths.
2018-05-22 23:15:08 +02:00
2022-07-16 11:59:20 +02:00
< CodeBlock example = "static-files" file = "directory.rs" section = "directory" / >
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
By default files listing for sub-directories is disabled. Attempt to load directory listing will return _404 Not Found_ response. To enable files listing, use [_Files::show_files_listing()_][showfileslisting] method.
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
Instead of showing files listing for directory, it is possible to redirect to a specific index file. Use the [_Files::index_file()_][indexfile] method to configure this redirect.
2018-07-21 15:33:25 +02:00
2022-07-16 11:59:20 +02:00
## Configuration
2018-07-21 15:33:25 +02:00
2019-06-26 10:27:25 +02:00
`NamedFiles` can specify various options for serving files:
2018-07-21 15:33:25 +02:00
2020-02-11 02:40:40 +01:00
- `set_content_disposition` - function to be used for mapping file's mime to corresponding `Content-Disposition` type
2019-06-26 10:27:25 +02:00
- `use_etag` - specifies whether `ETag` shall be calculated and included in headers.
2020-01-27 11:16:29 +01:00
- `use_last_modified` - specifies whether file modified timestamp should be used and added to `Last-Modified` header.
2018-07-21 15:33:25 +02:00
2022-02-26 05:41:49 +01:00
All of the above methods are optional and provided with the best defaults, But it is possible to customize any of them.
2018-07-21 15:33:25 +02:00
2022-07-16 11:59:20 +02:00
< CodeBlock example = "static-files" file = "configuration.rs" section = "config-one" / >
2018-07-21 15:33:25 +02:00
2019-06-26 10:27:25 +02:00
The Configuration can also be applied to directory service:
2018-07-21 15:33:25 +02:00
2022-07-16 11:59:20 +02:00
< CodeBlock example = "static-files" file = "configuration_two.rs" section = "config-two" / >
2019-06-25 05:36:32 +02:00
2019-12-28 19:36:24 +01:00
[showfileslisting]: https://docs.rs/actix-files/0.2/actix_files/struct.Files.html
[indexfile]: https://docs.rs/actix-files/0.2/actix_files/struct.Files.html#method.index_file