Course contentsShow
← Web Development
Lesson 97 of 2,9822. HTML FundamentalsFree lesson

File Input and Accept Attribute

Enabling file uploads with file type restrictions and multiple file selection.

File Input and Accept Attribute

You'll learn how to let users upload files to your form and control which file types they can choose.

What is a File Input?

The <input type="file"> element creates a special button that opens your computer's file picker dialog. When a user clicks it, they can browse their folders and select one or more files to upload with the form. Think of it like adding an attachment to an email—the file input is your "attach file" button.

Here's the basic syntax:

<form>
  <label for="resume">Upload your resume:</label>
  <input type="file" id="resume" name="resume">
</form>

The Accept Attribute: Setting File Type Restrictions

The accept attribute acts like a filter, guiding users toward appropriate file types. You specify which file extensions or MIME types are allowed:

<!-- Accept only image files -->
<input type="file" accept="image/*">

<!-- Accept specific file types -->
<input type="file" accept=".pdf,.doc,.docx">

<!-- Accept images and PDFs -->
<input type="file" accept="image/*,.pdf">

Important: The accept attribute is a guide, not enforcement. Users can sometimes override it, so always validate file types on the server side too.

Multiple File Selection

Adding the multiple attribute allows users to select more than one file at once:

<label for="photos">Upload photos:</label>
<input type="file" id="photos" name="photos" accept="image/*" multiple>

Now users can hold Ctrl (or Cmd on Mac) and click multiple files, or drag-and-drop several at once.

Key Takeaway: Use <input type="file"> with the accept attribute to enable file uploads while guiding users toward appropriate file types, and add multiple to allow selecting several files at once.