Html input file methods
The source for this interactive example is stored in a GitHub repository. If you’d like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Value
A file input’s value attribute contains a DOMString that represents the path to the selected file(s). If the user selected multiple files, the value represents the first file in the list of files they selected. The other files can be identified using the input’s HTMLInputElement.files property.
- If multiple files are selected, the string represents the first selected file. JavaScript can access the other files through the input’s files property.
- If no file is yet selected, the string is «» (empty).
- The string is prefixed with C:\fakepath\ , to prevent malicious software from guessing the user’s file structure.
Additional attributes
Attribute | Description |
---|---|
accept | One or more unique file type specifiers describing file types to allow |
capture | What source to use for capturing image or video data |
files | A FileList listing the chosen files |
multiple | A Boolean which, if present, indicates that the user may choose more than one file |
accept
The accept attribute value is a string that defines the file types the file input should accept. This string is a comma-separated list of unique file type specifiers. Because a given file type may be identified in more than one manner, it’s useful to provide a thorough set of type specifiers when you need files of a given format.
For instance, there are a number of ways Microsoft Word files can be identified, so a site that accepts Word files might use an like this:
capture
The capture attribute value is a string that specifies which camera to use for capture of image or video data, if the accept attribute indicates that the input should be of one of those types. A value of user indicates that the user-facing camera and/or microphone should be used. A value of environment specifies that the outward-facing camera and/or microphone should be used. If this attribute is missing, the user agent is free to decide on its own what to do. If the requested facing mode isn’t available, the user agent may fall back to its preferred default mode.
Note: capture was previously a Boolean attribute which, if present, requested that the device’s media capture device(s) such as camera or microphone be used instead of requesting a file input.
files
A FileList object that lists every selected file. This list has no more than one member unless the multiple attribute is specified.
multiple
When the multiple Boolean attribute is specified, the file input allows the user to select more than one file.
Non-standard attributes
In addition to the attributes listed above, the following non-standard attributes are available on some browsers. You should try to avoid using them when possible, since doing so will limit the ability of your code to function in browsers that don’t implement them.
Attribute | Description |
---|---|
webkitdirectory | A Boolean indicating whether or not to only allow the user to choose a directory (or directories, if multiple is also present) |
webkitdirectory
The Boolean webkitdirectory attribute, if present, indicates that only directories should be available to be selected by the user in the file picker interface. See HTMLInputElement.webkitdirectory for additional details and examples.
Note: Though originally implemented only for WebKit-based browsers, webkitdirectory is also usable in Microsoft Edge as well as Firefox 50 and later. However, even though it has relatively broad support, it is still not standard and should not be used unless you have no alternative.
Unique file type specifiers
A unique file type specifier is a string that describes a type of file that may be selected by the user in an element of type file . Each unique file type specifier may take one of the following forms:
- A valid case-insensitive filename extension, starting with a period («.») character. For example: .jpg , .pdf , or .doc .
- A valid MIME type string, with no extensions.
- The string audio/* meaning «any audio file».
- The string video/* meaning «any video file».
- The string image/* meaning «any image file».
The accept attribute takes as its value a string containing one or more of these unique file type specifiers, separated by commas. For example, a file picker that needs content that can be presented as an image, including both standard image formats and PDF files, might look like this: