You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5.9 KiB

📨 📁

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

/// info

📨 📂 📁, 🥇 python-multipart.

🤶 Ⓜ. pip install python-multipart.

👉 ↩️ 📂 📁 📨 "📨 💽".

///

🗄 File

🗄 File & UploadFile ➡️ fastapi:

{* ../../docs_src/request_files/tutorial001.py hl[1] *}

🔬 File 🔢

📁 🔢 🎏 🌌 👆 🔜 Body ⚖️ Form:

{* ../../docs_src/request_files/tutorial001.py hl[7] *}

/// info

File 🎓 👈 😖 🔗 ➡️ Form.

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

///

/// tip

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

///

📁 🔜 📂 "📨 💽".

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

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

📤 📚 💼 👆 💪 💰 ➡️ ⚙️ UploadFile.

📁 🔢 ⏮️ UploadFile

🔬 📁 🔢 ⏮️ 🆎 UploadFile:

{* ../../docs_src/request_files/tutorial001.py hl[12] *}

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

/// note | async 📡

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

///

/// note | 💃 📡

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

///

"📨 💽"

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

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

/// note | 📡

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

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

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

///

/// warning

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

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

///

📦 📁 📂

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

{* ../../docs_src/request_files/tutorial001_02.py hl[9,17] *}

UploadFile ⏮️ 🌖 🗃

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

{* ../../docs_src/request_files/tutorial001_03.py hl[13] *}

💗 📁 📂

💪 📂 📚 📁 🎏 🕰.

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

⚙️ 👈, 📣 📇 bytes ⚖️ UploadFile:

{* ../../docs_src/request_files/tutorial002.py hl[10,15] *}

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

/// note | 📡

👆 💪 ⚙️ from starlette.responses import HTMLResponse.

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

///

💗 📁 📂 ⏮️ 🌖 🗃

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

{* ../../docs_src/request_files/tutorial003.py hl[18] *}

🌃

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