Course contentsShow
← Web Development
Lesson 2665 of 2,98248. File Uploads and HandlingFree lesson

The File Input Element

Understanding <input type="file">, its attributes, and how it creates a file picker interface in the browser.

The File Input Element

You'll learn how the <input type="file"> element creates a browser-native file picker and what attributes control its behavior.

What It Does

The file input element is HTML's built-in way to let users select files from their device. When rendered, the browser creates a clickable button (usually labeled "Choose File" or "Browse") that opens the operating system's native file picker dialog. Once a user selects one or more files, JavaScript can access information about those files for processing or upload.

Think of it as a bridge between the user's file system and your web application—you're not directly accessing their hard drive, but rather asking the browser to safely mediate that interaction.

Basic Syntax and Key Attributes

<input type="file" name="document" id="fileInput">

Important attributes:

  • accept – Filters which file types appear in the picker (e.g., accept="image/*" or accept=".pdf,.doc")
  • multiple – Allows selecting multiple files at once
  • capture – On mobile, hints whether to use camera/microphone (e.g., capture="user" for front camera)
<!-- Accept only images, allow multiple -->
<input type="file" accept="image/png,image/jpeg" multiple>

<!-- Suggest using the camera on mobile -->
<input type="file" accept="image/*" capture="environment">

How It Connects to JavaScript

Once a file is selected, you can access it through the element's files property—a FileList object containing File objects. These provide metadata like name, size, and type, which you've prepared for in earlier form handling lessons.

Key Takeaway: The <input type="file"> creates a secure, browser-controlled interface for file selection, configurable through attributes like accept, multiple, and capture, and accessible via JavaScript's files property.