靜態檔案 - 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(取得回應) async(非同步)
¶
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(檢查設定) async(非同步)
¶
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 |
|