Перейти к основному содержимому
← 📄 HTML & the platform·Модуль A3 · Урок 10
ЗаданиеBuild an upload form: enctype='multipart/form-data', method='post', action='/api/upload'. <input type='file' name='photo' accept='image/*'>, <input type='file' name='docs' multiple accept='.pdf'>, submit button.

<тип ввода=файл>: загрузки + составная часть

100 XP8 min
Теория

File uploads need multipart enctype

<form action="/api/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="avatar" />
  <input type="file" name="docs" multiple accept=".pdf,.docx" />
  <button type="submit">Upload</button>
</form>

Two changes vs a regular form:

  1. `enctype="multipart/form-data"` — without it, the browser only submits the filename, not the file content. Always set this on forms with file inputs.
  2. `accept=…` on the file input — restricts the OS picker to matching files. Either MIME types (image/png) or extensions (.pdf).

multiple

<input multiple> lets the user pick more than one file at once. Submitted as repeated form keys (same name, multiple files).

Accept patterns

<input type="file" accept="image/*" />              <!-- any image MIME -->
<input type="file" accept=".jpg,.png,.webp" />      <!-- specific extensions -->
<input type="file" accept="image/png,image/jpeg" /> <!-- specific MIMEs -->
<input type="file" accept="application/pdf" />

accept is a HINT — browsers honor it loosely. Backend MUST re-validate by reading the actual file content (magic bytes), not just the extension.

FastAPI side

from fastapi import UploadFile

@app.post("/api/upload")
async def upload(avatar: UploadFile, docs: list[UploadFile] = []):
    return {"avatar": avatar.filename, "docs": [d.filename for d in docs]}

The form's name="avatar" becomes the function arg. multiple becomes list[UploadFile].

🔒

Зарегистрируйся чтобы кодить

Теория открыта всем. Интерактивный редактор, живой превью и проверка открываются с 7-дневным бесплатным trial — нужна карта, отмена в любое время.

Регистрация — бесплатный trial →

Первые 10 уроков в каждом треке — бесплатные. Карта не нужна.

← НазадСледующий урок →

Получай один совет по Python или веб-разработке — каждый день в почте

Коротко, без воды, без спама. Отписаться — в один клик.