跳至內容

UploadFile 類別

您可以定義路徑操作函式的參數類型為 UploadFile 來接收請求中的檔案。

您可以直接從 fastapi 導入它。

from fastapi import UploadFile

fastapi.UploadFile

UploadFile(file, *, size=None, filename=None, headers=None)

基底:UploadFile

請求中上傳的檔案。

將其定義為路徑操作函式(或依賴項)的參數。

如果您使用的是普通的 def 函式,您可以使用 upload_file.file 屬性來訪問原始的標準 Python 檔案(阻塞式,非異步),這對於非異步程式碼很有用且是必要的。

FastAPI 的請求檔案文件中了解更多資訊。

範例

from typing import Annotated

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.post("/files/")
async def create_file(file: Annotated[bytes, File()]):
    return {"file_size": len(file)}


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
    return {"filename": file.filename}
參數 說明
file (檔案)

類型: BinaryIO

size (大小)

類型: int | None 預設值: None

filename (檔案名稱)

類型: str | None 預設值: None

headers (標頭)

類型: Headers | None 預設值: None

原始碼位於 starlette/datastructures.py
414
415
416
417
418
419
420
421
422
423
424
425
def __init__(
    self,
    file: typing.BinaryIO,
    *,
    size: int | None = None,
    filename: str | None = None,
    headers: Headers | None = None,
) -> None:
    self.filename = filename
    self.file = file
    self.size = size
    self.headers = headers or Headers()

file 實體屬性

file

標準的 Python 檔案物件(非異步)。

filename 實體屬性

filename

原始檔案名稱。

size 實體屬性

size

檔案大小,單位為位元組。

headers 實體屬性

headers

請求的標頭。

content_type 實體屬性

content_type

請求的內容類型,來自標頭。

read 非同步

read(size=-1)

從檔案中讀取一些位元組。

為了使其可等待,並與非同步相容,此操作在執行緒池中運行。

參數 說明
size (大小)

要從檔案中讀取的位元組數。

類型: int 預設值: -1

原始碼位於 fastapi/datastructures.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
async def read(
    self,
    size: Annotated[
        int,
        Doc(
            """
            The number of bytes to read from the file.
            """
        ),
    ] = -1,
) -> bytes:
    """
    Read some bytes from the file.

    To be awaitable, compatible with async, this is run in threadpool.
    """
    return await super().read(size)

write async

write(data)

寫入一些位元組到檔案。

通常您不會對從請求中讀取的檔案使用此方法。

為了使其可等待,並與非同步相容,此操作在執行緒池中運行。

參數 說明
data

要寫入檔案的位元組。

類型: bytes

原始碼位於 fastapi/datastructures.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
async def write(
    self,
    data: Annotated[
        bytes,
        Doc(
            """
            The bytes to write to the file.
            """
        ),
    ],
) -> None:
    """
    Write some bytes to the file.

    You normally wouldn't use this from a file you read in a request.

    To be awaitable, compatible with async, this is run in threadpool.
    """
    return await super().write(data)

seek async

seek(offset)

移動到檔案中的某個位置。

任何後續的讀取或寫入都將從該位置開始。

為了使其可等待,並與非同步相容,此操作在執行緒池中運行。

參數 說明
offset

要在檔案中搜尋的位元組位置。

類型: int

原始碼位於 fastapi/datastructures.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
async def seek(
    self,
    offset: Annotated[
        int,
        Doc(
            """
            The position in bytes to seek to in the file.
            """
        ),
    ],
) -> None:
    """
    Move to a position in the file.

    Any next read or write will be done from that position.

    To be awaitable, compatible with async, this is run in threadpool.
    """
    return await super().seek(offset)

close async

close()

關閉檔案。

為了使其可等待,並與非同步相容,此操作在執行緒池中運行。

原始碼位於 fastapi/datastructures.py
133
134
135
136
137
138
139
async def close(self) -> None:
    """
    Close the file.

    To be awaitable, compatible with async, this is run in threadpool.
    """
    return await super().close()