W3cubDocs

/Web APIs

HTMLInputElement: webkitdirectory property

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.

Note: This property is called webkitEntries in the specification due to its origins as a Google Chrome-specific API. It's likely to be renamed someday.

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 PhotoAlbums/Birthdays/Don's 40th birthday/PIC2343.jpg. This makes it possible to know the hierarchy even though the FileList is flat.

Note: The behavior of webkitRelativePath is different in Chromium < 72. See this bug for further details.

Examples

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

html

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

JavaScript

js

document.getElementById("filepicker").addEventListener(
  "change",
  (event) => {
    let output = document.getElementById("listing");
    for (const file of event.target.files) {
      let item = document.createElement("li");
      item.textContent = file.webkitRelativePath;
      output.appendChild(item);
    }
  },
  false,
);

Result

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
webkitdirectory 7 13 50 No 15 11.1 4.4 18 No 14 11.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/webkitdirectory