跳到内容

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}
源代码在 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 instance-attribute

file

标准的 Python 文件对象(非异步)。

filename instance-attribute

filename

原始文件名。

size instance-attribute

size

文件大小(字节)。

headers instance-attribute

headers

请求的请求头。

content_type instance-attribute

content_type

请求的内容类型,来自请求头。

read async

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()