1
0
mirror of https://github.com/actix/actix-website synced 2024-11-24 08:43:01 +01:00
actix-website/content/docs/static-files.md

50 lines
2.0 KiB
Markdown
Raw Normal View History

2018-05-22 23:15:08 +02:00
---
title: Static Files
menu: docs_advanced
weight: 230
---
# Individual file
It is possible to serve static files with a custom path pattern and `NamedFile`. To
match a path tail, we can use a `[.*]` regex.
2019-06-17 23:35:47 +02:00
{{< include-example example="static-files" file="main.rs" section="individual-file" >}}
2018-05-22 23:15:08 +02:00
# Directory
2019-06-17 23:35:47 +02:00
To serve files from specific directories and sub-directories, `Files` can be used.
`Files` must be registered with an `App::service()` method, otherwise
2018-05-22 23:15:08 +02:00
it will be unable to serve sub-paths.
2019-06-17 23:35:47 +02:00
{{< include-example example="static-files" file="directory.rs" section="directory" >}}
2018-05-22 23:15:08 +02:00
2019-06-17 23:35:47 +02: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()*](https://docs.rs/actix-files/0.1.2/actix_files/struct.Files.html)
2018-05-22 23:15:08 +02:00
method.
Instead of showing files listing for directory, it is possible to redirect
to a specific index file. Use the
2019-06-17 23:35:47 +02:00
[*Files::index_file()*](https://docs.rs/actix-files/0.1.2/actix_files/struct.Files.html#method.index_file)
2018-05-22 23:15:08 +02:00
method to configure this redirect.
2018-07-21 15:33:25 +02:00
# Configuration
Generic trait `StaticFileConfig` can be used to specify various options
for serving files:
- `content_disposition_map` - function to be used for mapping file's mime to corresponding `Content-Disposition` type
- `is_use_etag` - specifies whether `ETag` shall be calculated and included in headers.
- `is_use_last_modifier` - specifies whether file modified timestamp should be used and added to `Last-Modified` header.
- `is_method_allowed` - allows to control which HTTP methods are allowed to be used when accessing file.
All of the above methods are optional and provided with the best defaults.
But it is possible to customize any of them by implementing the trait onto own struct.
2019-06-17 23:35:47 +02:00
{{< include-example example="static-files" file="configuration.rs" section="config-one" >}}
2018-07-21 15:33:25 +02:00
The Configuration cal also be applied to directory service:
2019-06-17 23:35:47 +02:00
{{< include-example example="static-files" file="configuration_two.rs" section="config-two" >}}