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/*"oraccept=".pdf,.doc")multiple– Allows selecting multiple files at oncecapture– 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.