HTML File Upload

File Upload in HTML Forms: Let Users Send Their Files

Whether it’s uploading a resume, a photo of a cherry, or a PDF recipe, HTML's <input type="file"> makes it possible. This control allows users to select one or more files from their device and send them through a form.

Basic Syntax for File Upload

The simplest file input accepts a single file of any type:

<form action="/upload" method="post" enctype="multipart/form-data">
  <label for="file">Upload a file:</label><br>
  <input type="file" id="file" name="fruitFile"><br><br>
  <input type="submit" value="Upload">
</form>
Upload a file: [Choose File] No file chosen  
[Upload]

Important: Always use enctype="multipart/form-data" in the form tag when uploading files. This tells the browser to send binary data correctly.

Accepting Specific File Types

To limit what users can upload, use the accept attribute with file extensions or MIME types.

Images only:

<input type="file" name="fruitImage" accept="image/*">

PDFs only:

<input type="file" name="doc" accept=".pdf">

CSV or Text files:

<input type="file" name="dataFile" accept=".csv, .txt">
Choose File: Only matching types are selectable

This prevents accidental uploads of unsupported file types and streamlines user choices.

Uploading Multiple Files

To allow users to select more than one file, add the multiple attribute:

<input type="file" name="fruitPhotos" accept="image/*" multiple>
Choose File: (you can select multiple images)

All selected files will be submitted under the same input name.

Real-World Example: Upload Fruit Photos

<form action="/submit-fruit-gallery" method="post" enctype="multipart/form-data">

  <label for="applePic">Upload an apple photo:</label><br>
  <input type="file" id="applePic" name="applePic" accept="image/*"><br><br>

  <label for="bananaFiles">Upload multiple banana images:</label><br>
  <input type="file" id="bananaFiles" name="bananaPhotos" accept="image/*" multiple><br><br>

  <label for="cherryPDF">Upload cherry info (PDF only):</label><br>
  <input type="file" id="cherryPDF" name="cherryDoc" accept=".pdf"><br><br>

  <input type="submit" value="Submit Gallery">

</form>
[Choose File] Apple photo  
[Choose Files] Banana images  
[Choose File] Cherry PDF  
[Submit Gallery]

Best Practices

  • Always use enctype="multipart/form-data" on the form
  • Use accept to limit file types and prevent user confusion
  • Apply multiple only when needed to avoid clutter
  • Label clearly so users know what file they’re uploading

Browser Support

All major modern browsers support file input fields. If unsupported, users may see a basic text field — but this is rare today.

Summary

HTML file inputs make it easy for users to send images, documents, and more. You’ve now learned how to:

  • Use <input type="file">
  • Restrict file types using accept
  • Enable multiple file uploads
  • Structure forms for file transmission

Check your understanding 1

Which HTML input type allows users to upload files from their device?
Choose one answer. You can try again.

Answer and explanation

<code>type="file"</code>

Use type="file" to create a file selection button that lets users upload documents, images, or any local files.

Check your understanding 2

Using the multiple attribute in a file input allows users to select more than one file at a time.
Choose one answer. You can try again.

Answer and explanation

True

The multiple attribute extends the file input to accept multiple selections — ideal for uploading a set of homework files or project resources.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. <input type="file" accept=".jpg,.png">
  2. <input type="file" accept="image/*">
  3. <input type="file" types="pdf">
  4. <input type="file" accept="application/pdf">
Which of the following are valid ways to restrict uploads to specific file types in HTML?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 4

The accept attribute supports MIME types like application/pdf, image/*, or extensions like .jpg. There is no attribute called types.

Check your understanding 4

What does the following code do? <input type="file" name="assignment" accept=".pdf">
Choose one answer. You can try again.

Answer and explanation

Only allows uploading files with a .pdf extension

The accept attribute limits the browser file picker to show only PDF files — a useful constraint for assignment or ID uploads.

Check your understanding 5

You can upload a file using HTML only, without needing a form or backend to handle it.
Choose one answer. You can try again.

Answer and explanation

False

HTML can capture a file, but processing it (like saving or emailing) requires a server-side script or integration with JavaScript for further handling.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. Uploading scanned report cards
  2. Choosing a background color for UI
  3. Submitting practical lab images
  4. Attaching student ID proofs
Which school-related use cases can utilize the file upload input in a form?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 3 and 4

File uploads are ideal for anything involving document or media attachments — like submitting lab work or verifying documents. UI color selection is better suited for type="color".

What’s Next?

Next, let’s discover how Datalist inputs offer flexible suggestions without limiting user freedom — the perfect blend of control and creativity.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML Datalist →