Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

HTMLInputElement.webkitdirectory

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

The HTMLInputElement.webkitdirectory is a property that reflects the webkitdirectory HTML attribute and indicates that the <input> element should let the user select directories instead of files. When a directory is selected, the directory and its entire hierarchy of contents are included in the set of selected items. The selected file system entries can be obtained using the webkitEntries property.

Syntax

 HTMLInputElement.webkitdirectory = boolValue

Value

A Boolean; true if the <input> element should allow picking only directories or false if only files should be selectable.

Understanding the results

After the user makes a selection, each File object in files has its File.webkitRelativePath property set to the relative path within the selected directory at which the file is located. For example, consider this file system:

  • PhotoAlbums
    • Birthdays
      • Jamie's 1st birthday
        • PIC1000.jpg
        • PIC1004.jpg
        • PIC1044.jpg
      • Don's 40th birthday
        • PIC2343.jpg
        • PIC2344.jpg
        • PIC2355.jpg
        • PIC2356.jpg
    • Vacations
      • Mars
        • PIC5533.jpg
        • PIC5534.jpg
        • PIC5556.jpg
        • PIC5684.jpg
        • PIC5712.jpg

If the user chooses "PhotoAlbums", then the list reported by files will contain File objects for every file listed above—but not the directories. The entry for PIC2343.jpg will have a webkitRelativePath of "/Birthdays/Don's 40th birthday/PIC2343.jpg". This makes it possible to know the hierarchy even though the FileList is flat.

Example

In this example, a directory picker is presented which lets the user choose one or more directories. When the change event occurs, a list of all files contained within the selected directory hierarchies is generated and displayed.

HTML content

<input type="file" id="filepicker" name="fileList" webkitdirectory multiple />
<ul id="listing"></ul>

JavaScript content

document.getElementById("filepicker").addEventListener("change", function(event) {
  let output = document.getElementById("listing");
  let files = event.target.files;

  for (let i=0; i<files.length; i++) {
    let item = document.createElement("li");
    item.innerHTML = files[i].webkitRelativePath;
    output.appendChild(item);
  };
}, false);

Result

Specifications

Specification Status Comment
File and Directory Entries API
The definition of 'webkitdirectory' in that specification.
Editor's Draft Initial specification.

This API has no official W3C or WHATWG specification.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Microsoft Edge Opera Safari (WebKit)
Basic support 13 webkit 50 (50)[1] No support (Yes) No support No support
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support No support 0.16 webkit 50.0 (50) No support No support No support

[1] While setting allowdir to true results in HTMLInputElement.files being null (because the user has chosen to act on the directory itself, rather than the files within), setting webkitdirectory to true causes the files property to be a FileList containing every file and every file contained within the hierarchy of contents of every directory selected. allowdir is a Firefox specific property.

See also

Document Tags and Contributors

 Contributors to this page: Sheppy, teoli
 Last updated by: Sheppy,