W3cubDocs

/Web APIs

HTMLInputElement: files property

The HTMLInputElement.files property allows you to access the FileList selected with the <input type="file"> element.

Value

A FileList object listing the selected files, if any, or null if the HTMLInputElement is not of type="file".

Examples

The example below shows how you can access the HTMLInputElement.files property and log the name, date modified, size, and type of each file selected by the user.

HTML

html

<input id="files" type="file" multiple />

JavaScript

Note that HTMLInputElement.files still returns an instance of FileList even if no files are selected. Therefore it's safe to iterate through it with for...of without checking if any files are selected.

js

const fileInput = document.getElementById("files");

console.log(fileInput.files instanceof FileList); // true even if empty

for (const file of fileInput.files) {
  console.log(file.name); // prints file name
  let fileDate = new Date(file.lastModified);
  console.log(fileDate.toLocaleDateString()); // prints legible date
  console.log(
    file.size < 1000 ? file.size : Math.round(file.size / 1000) + "KB",
  );
  console.log(file.type); // prints MIME type
}

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
files 2 12 3
10This property is read-only.
≤12.1 4 ≤37 18 4 ≤12.1 3 1.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/files