静态文件 - StaticFiles
¶
你可以使用 StaticFiles
类来服务静态文件,例如 JavaScript、CSS、图像等。
在 FastAPI 文档的静态文件部分 中了解更多信息。
你可以直接从 fastapi.staticfiles
中导入它
from fastapi.staticfiles import StaticFiles
fastapi.staticfiles.StaticFiles ¶
StaticFiles(
*,
directory=None,
packages=None,
html=False,
check_dir=True,
follow_symlink=False
)
参数 | 描述 |
---|---|
directory
|
类型: |
packages
|
类型: |
html
|
类型: |
check_dir
|
类型: |
follow_symlink
|
类型: |
源代码在 starlette/staticfiles.py
中
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
get_directories ¶
get_directories(directory=None, packages=None)
给定 directory
和 packages
参数,返回一个列表,其中包含用于从其提供静态文件的目录。
参数 | 描述 |
---|---|
directory
|
类型: |
packages
|
类型: |
源代码在 starlette/staticfiles.py
中
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
get_path ¶
get_path(scope)
给定 ASGI 作用域,返回要提供的 path
字符串,带有操作系统特定的路径分隔符,并删除所有 ' ..' 和 '.' 组件。
参数 | 描述 |
---|---|
scope
|
类型: |
源代码在 starlette/staticfiles.py
中
101 102 103 104 105 106 107 |
|
get_response 异步
¶
get_response(path, scope)
给定传入路径、方法和请求头,返回 HTTP 响应。
参数 | 描述 |
---|---|
path
|
类型: |
scope
|
类型: |
源代码在 starlette/staticfiles.py
中
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
lookup_path ¶
lookup_path(path)
参数 | 描述 |
---|---|
path
|
类型: |
源代码在 starlette/staticfiles.py
中
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
file_response ¶
file_response(
full_path, stat_result, scope, status_code=200
)
参数 | 描述 |
---|---|
full_path
|
类型: |
stat_result
|
类型: |
scope
|
类型: |
status_code
|
类型: |
源代码在 starlette/staticfiles.py
中
169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
check_config 异步
¶
check_config()
执行一次性配置检查,以确保 StaticFiles 实际指向一个目录,这样我们可以抛出明显的错误,而不是仅仅返回 404 响应。
源代码在 starlette/staticfiles.py
中
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
is_not_modified ¶
is_not_modified(response_headers, request_headers)
给定请求和响应头,如果可以返回 HTTP "未修改" 响应,则返回 True
。
参数 | 描述 |
---|---|
response_headers
|
类型: |
request_headers
|
类型: |
源代码在 starlette/staticfiles.py
中
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|