Skip to content

📨 📁

👆 💪 🔬 📁 📂 👩‍💻 ⚙️ File.

Info

📨 📂 📁, 🥇 ❎ python-multipart.

🤶 Ⓜ. pip install python-multipart.

👉 ↩️ 📂 📁 📨 "📨 💽".

🗄 File

🗄 File & UploadFile ⚪️➡️ fastapi:

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


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


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
    return {"filename": file.filename}

🔬 File 🔢

✍ 📁 🔢 🎏 🌌 👆 🔜 Body ⚖️ Form:

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


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


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
    return {"filename": file.filename}

Info

File 🎓 👈 😖 🔗 ⚪️➡️ Form.

✋️ 💭 👈 🕐❔ 👆 🗄 Query, Path, File & 🎏 ⚪️➡️ fastapi, 👈 🤙 🔢 👈 📨 🎁 🎓.

Tip

📣 📁 💪, 👆 💪 ⚙️ File, ↩️ ⏪ 🔢 🔜 🔬 🔢 🔢 ⚖️ 💪 (🎻) 🔢.

📁 🔜 📂 "📨 💽".

🚥 👆 📣 🆎 👆 ➡ 🛠️ 🔢 🔢 bytes, FastAPI 🔜 ✍ 📁 👆 & 👆 🔜 📨 🎚 bytes.

✔️ 🤯 👈 👉 ⛓ 👈 🎂 🎚 🔜 🏪 💾. 👉 🔜 👷 👍 🤪 📁.

✋️ 📤 📚 💼 ❔ 👆 💪 💰 ⚪️➡️ ⚙️ UploadFile.

📁 🔢 ⏮️ UploadFile

🔬 📁 🔢 ⏮️ 🆎 UploadFile:

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


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


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
    return {"filename": file.filename}

⚙️ UploadFile ✔️ 📚 📈 🤭 bytes:

  • 👆 🚫 ✔️ ⚙️ File() 🔢 💲 🔢.
  • ⚫️ ⚙️ "🧵" 📁:
    • 📁 🏪 💾 🆙 🔆 📐 📉, & ⏮️ 🚶‍♀️ 👉 📉 ⚫️ 🔜 🏪 💾.
  • 👉 ⛓ 👈 ⚫️ 🔜 👷 👍 ⭕ 📁 💖 🖼, 📹, ⭕ 💱, ♒️. 🍵 😩 🌐 💾.
  • 👆 💪 🤚 🗃 ⚪️➡️ 📂 📁.
  • ⚫️ ✔️ 📁-💖 async 🔢.
  • ⚫️ 🎦 ☑ 🐍 SpooledTemporaryFile 🎚 👈 👆 💪 🚶‍♀️ 🔗 🎏 🗃 👈 ⌛ 📁-💖 🎚.

UploadFile

UploadFile ✔️ 📄 🔢:

  • filename: str ⏮️ ⏮️ 📁 📛 👈 📂 (✅ myimage.jpg).
  • content_type: str ⏮️ 🎚 🆎 (📁 🆎 / 📻 🆎) (✅ image/jpeg).
  • file: SpooledTemporaryFile ( 📁-💖 🎚). 👉 ☑ 🐍 📁 👈 👆 💪 🚶‍♀️ 🔗 🎏 🔢 ⚖️ 🗃 👈 ⌛ "📁-💖" 🎚.

UploadFile ✔️ 📄 async 👩‍🔬. 👫 🌐 🤙 🔗 📁 👩‍🔬 🔘 (⚙️ 🔗 SpooledTemporaryFile).

  • write(data): ✍ data (str ⚖️ bytes) 📁.
  • read(size): ✍ size (int) 🔢/🦹 📁.
  • seek(offset): 🚶 🔢 🧘 offset (int) 📁.
    • 🤶 Ⓜ., await myfile.seek(0) 🔜 🚶 ▶️ 📁.
    • 👉 ✴️ ⚠ 🚥 👆 🏃 await myfile.read() 🕐 & ⤴️ 💪 ✍ 🎚 🔄.
  • close(): 🔐 📁.

🌐 👫 👩‍🔬 async 👩‍🔬, 👆 💪 "⌛" 👫.

🖼, 🔘 async ➡ 🛠️ 🔢 👆 💪 🤚 🎚 ⏮️:

contents = await myfile.read()

🚥 👆 🔘 😐 def ➡ 🛠️ 🔢, 👆 💪 🔐 UploadFile.file 🔗, 🖼:

contents = myfile.file.read()

"async 📡 ℹ"

🕐❔ 👆 ⚙️ async 👩‍🔬, FastAPI 🏃 📁 👩‍🔬 🧵 & ⌛ 👫.

"💃 📡 ℹ"

FastAPI'Ⓜ UploadFile 😖 🔗 ⚪️➡️ 💃'Ⓜ UploadFile, ✋️ 🚮 💪 🍕 ⚒ ⚫️ 🔗 ⏮️ Pydantic & 🎏 🍕 FastAPI.

⚫️❔ "📨 💽"

🌌 🕸 📨 (<form></form>) 📨 💽 💽 🛎 ⚙️ "🎁" 🔢 👈 📊, ⚫️ 🎏 ⚪️➡️ 🎻.

FastAPI 🔜 ⚒ 💭 ✍ 👈 📊 ⚪️➡️ ▶️️ 🥉 ↩️ 🎻.

"📡 ℹ"

📊 ⚪️➡️ 📨 🛎 🗜 ⚙️ "📻 🆎" application/x-www-form-urlencoded 🕐❔ ⚫️ 🚫 🔌 📁.

✋️ 🕐❔ 📨 🔌 📁, ⚫️ 🗜 multipart/form-data. 🚥 👆 ⚙️ File, FastAPI 🔜 💭 ⚫️ ✔️ 🤚 📁 ⚪️➡️ ☑ 🍕 💪.

🚥 👆 💚 ✍ 🌖 🔃 👉 🔢 & 📨 🏑, 👳 🏇 🕸 🩺 POST.

Warning

👆 💪 📣 💗 File & Form 🔢 ➡ 🛠️, ✋️ 👆 💪 🚫 📣 Body 🏑 👈 👆 ⌛ 📨 🎻, 📨 🔜 ✔️ 💪 🗜 ⚙️ multipart/form-data ↩️ application/json.

👉 🚫 🚫 FastAPI, ⚫️ 🍕 🇺🇸🔍 🛠️.

📦 📁 📂

👆 💪 ⚒ 📁 📦 ⚙️ 🐩 🆎 ✍ & ⚒ 🔢 💲 None:

from typing import Union

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.post("/files/")
async def create_file(file: Union[bytes, None] = File(default=None)):
    if not file:
        return {"message": "No file sent"}
    else:
        return {"file_size": len(file)}


@app.post("/uploadfile/")
async def create_upload_file(file: Union[UploadFile, None] = None):
    if not file:
        return {"message": "No upload file sent"}
    else:
        return {"filename": file.filename}
from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.post("/files/")
async def create_file(file: bytes | None = File(default=None)):
    if not file:
        return {"message": "No file sent"}
    else:
        return {"file_size": len(file)}


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile | None = None):
    if not file:
        return {"message": "No upload file sent"}
    else:
        return {"filename": file.filename}

UploadFile ⏮️ 🌖 🗃

👆 💪 ⚙️ File() ⏮️ UploadFile, 🖼, ⚒ 🌖 🗃:

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.post("/files/")
async def create_file(file: bytes = File(description="A file read as bytes")):
    return {"file_size": len(file)}


@app.post("/uploadfile/")
async def create_upload_file(
    file: UploadFile = File(description="A file read as UploadFile"),
):
    return {"filename": file.filename}

💗 📁 📂

⚫️ 💪 📂 📚 📁 🎏 🕰.

👫 🔜 👨‍💼 🎏 "📨 🏑" 📨 ⚙️ "📨 💽".

⚙️ 👈, 📣 📇 bytes ⚖️ UploadFile:

from typing import List

from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.post("/files/")
async def create_files(files: List[bytes] = File()):
    return {"file_sizes": [len(file) for file in files]}


@app.post("/uploadfiles/")
async def create_upload_files(files: List[UploadFile]):
    return {"filenames": [file.filename for file in files]}


@app.get("/")
async def main():
    content = """
<body>
<form action="/files/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
<form action="/uploadfiles/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
</body>
    """
    return HTMLResponse(content=content)
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.post("/files/")
async def create_files(files: list[bytes] = File()):
    return {"file_sizes": [len(file) for file in files]}


@app.post("/uploadfiles/")
async def create_upload_files(files: list[UploadFile]):
    return {"filenames": [file.filename for file in files]}


@app.get("/")
async def main():
    content = """
<body>
<form action="/files/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
<form action="/uploadfiles/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
</body>
    """
    return HTMLResponse(content=content)

👆 🔜 📨, 📣, list bytes ⚖️ UploadFileⓂ.

"📡 ℹ"

👆 💪 ⚙️ from starlette.responses import HTMLResponse.

FastAPI 🚚 🎏 starlette.responses fastapi.responses 🏪 👆, 👩‍💻. ✋️ 🌅 💪 📨 👟 🔗 ⚪️➡️ 💃.

💗 📁 📂 ⏮️ 🌖 🗃

& 🎏 🌌 ⏭, 👆 💪 ⚙️ File() ⚒ 🌖 🔢, UploadFile:

from typing import List

from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.post("/files/")
async def create_files(
    files: List[bytes] = File(description="Multiple files as bytes"),
):
    return {"file_sizes": [len(file) for file in files]}


@app.post("/uploadfiles/")
async def create_upload_files(
    files: List[UploadFile] = File(description="Multiple files as UploadFile"),
):
    return {"filenames": [file.filename for file in files]}


@app.get("/")
async def main():
    content = """
<body>
<form action="/files/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
<form action="/uploadfiles/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
</body>
    """
    return HTMLResponse(content=content)
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.post("/files/")
async def create_files(
    files: list[bytes] = File(description="Multiple files as bytes"),
):
    return {"file_sizes": [len(file) for file in files]}


@app.post("/uploadfiles/")
async def create_upload_files(
    files: list[UploadFile] = File(description="Multiple files as UploadFile"),
):
    return {"filenames": [file.filename for file in files]}


@app.get("/")
async def main():
    content = """
<body>
<form action="/files/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
<form action="/uploadfiles/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
</body>
    """
    return HTMLResponse(content=content)

🌃

⚙️ File, bytes, & UploadFile 📣 📁 📂 📨, 📨 📨 💽.