Accessing Selected Files
You'll learn how to read the FileList from a file input and inspect each File object's key properties.
The FileList Object
When a user selects files using <input type="file">, the browser populates the input's files property with a FileList—an array-like collection of File objects. You access this list via inputElement.files in JavaScript. Though it looks like an array, it's technically a FileList, so some array methods won't work directly (though you can convert it).
Reading File Properties
Each File object in the list contains metadata about the selected file:
const fileInput = document.querySelector('#myFileInput');
fileInput.addEventListener('change', (event) => {
const files = event.target.files; // FileList object
if (files.length > 0) {
const firstFile = files[0]; // Get first File object
console.log(firstFile.name); // e.g., "photo.jpg"
console.log(firstFile.size); // Size in bytes, e.g., 204800
console.log(firstFile.type); // MIME type, e.g., "image/jpeg"
}
});
Key Properties
name: The filename (string) without the full path for security reasonssize: File size in bytes (number)—useful for validation before uploadtype: The MIME type (string) like"image/png"or"application/pdf", though it can be empty if the browser can't determine it
Think of the FileList as a shopping bag of receipts: you can peek at each receipt's label (name), weight (size), and category (type) before deciding how to process it.
Key Takeaway: Access selected files through inputElement.files, which returns a FileList containing File objects with name, size, and type properties—essential metadata for validation and processing before upload.