Beginner System Architecture

How a File Upload Works

When you pick a file in a form and hit submit, the browser packages that file and sends it to the server inside an HTTP request. Understanding the steps helps you handle large files, set sane limits, and store data safely.

multipart/form-data

Text fields are simple, but files contain binary data. To send both together, browsers use the multipart/form-data encoding. Each field becomes a “part” with its own headers, separated by a boundary string.

POST /upload HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary=----X

------X
Content-Disposition: form-data; name="title"

My holiday photo
------X
Content-Disposition: form-data; name="file"; filename="beach.jpg"
Content-Type: image/jpeg

(binary bytes of the image here)
------X--

The form must declare the encoding.

<form method="post" action="/upload" enctype="multipart/form-data">

Streaming the file

A large file should not be loaded fully into memory. Servers read it in chunks and write each chunk straight to disk or to remote storage. This keeps memory use low even for big files.

Request body --> [chunk] --> [chunk] --> [chunk] --> storage
        (read a piece, write a piece, repeat)

Limits and validation

Always set and check limits, both for safety and stability.

  • Max file size, to avoid filling the disk or exhausting memory.
  • Allowed types, checked by content, not just the file extension.
  • Number of files per request.
  • Authentication, so only allowed users upload.
if size > MAX_SIZE: reject 413 Payload Too Large
if type not in ALLOWED: reject 415 Unsupported Media Type

Where to store it

  • Local disk: simple, but does not scale across many servers.
  • Object storage (like S3): scalable, durable, the common choice.
  • Database: usually a bad fit for large binaries; store a reference instead.

A frequent pattern: save the file in object storage and keep only its URL and metadata in the database.

Advantages and trade-offs

  • Streaming keeps memory low and supports large files.
  • Object storage scales and survives server loss.
  • The cost is more moving parts and careful validation.

When it matters

Any system that accepts user files (images, documents, videos) needs this. Get the limits and storage right early; retrofitting them after a disk fills up is painful.