\n\nThis provides relative paths regarding the directory's structure, without revealing the full file system path.","image":"https://explain.codes/media/static/images/eightify-logo.svg","author":{"@type":"Person","name":"Nikita Barsukov","url":"https://explain.codes//author/nikita-barsukov"},"publisher":{"@type":"Organization","name":"Rational Expressions, Inc","logo":{"@type":"ImageObject","url":"https://explain.codes/landing/images/[email protected]"}},"datePublished":"2025-01-10T18:00:01.464Z","dateModified":"2025-01-10T18:00:03.460Z"}
Explain Codes LogoExplain Codes Logo

How to get folder directory from HTML input type "file" or any other way?

javascript
file-system-access
directory-selection
web-api
Nikita BarsukovbyNikita Barsukov·Jan 10, 2025
TLDR
HTML does not expose file paths for privacy. You can use the `<input type="file" webkitdirectory>` attribute and handle the files with JavaScript:

<input type="file" webkitdirectory directory onchange="handleFiles(this.files)">

<script>
  function handleFiles(files) {
    for (let file of files) {
      console.log(`Oh look, a file: ${file.name}, nestled in the folder: ${file.webkitRelativePath}`);
    }
  }
</script>

This provides relative paths regarding the directory's structure, without revealing the full file system path.

A sweeping look at folder access across the eras

From the age of dinosaurs (okay, I exaggerate, I mean technologies like Flash, Silverlight and ActiveX 😉), to the modern era of JavaScript and HTML, we've seen various ways of handling file and folder selection. Interestingly enough, despite the evolution, secure access and privacy protection have remained a constant priority.

Now <input type="file" webkitdirectory> allows for file and directory selection, and in a turn for the better, File System Access API enhances folder interaction even more robustly.

Web API and JavaScript: A dynamic duo

What's a hero without a sidekick? JavaScript and modern web APIs combine forces in managing file selection and post-selection data manipulation. With JavaScript, developers can parse selected files retrieved by the input element and perform a range of operations.

Modern web APIs bring a breath of fresh air with enhanced approaches to local file and folder access, leading us to the File System Access API and the window.showDirectoryPicker() method. Superheroes in action!

File System Access API: Unlocking possibilities

Your magic key to more structured local file and directory access:

async function getDirectoryHandle() { const directoryHandle = await window.showDirectoryPicker(); // Wow! You've just got the directory handle! }

That's a directory handle at your fingertips, paving the way to asynchronous file operations. Neat, isn't it?

Balancing usability and user protection

Privacy first

Although new features promote usability, they often raise privacy and security concerns. Hence, the sandboxing of browsers prevents web pages from freely accessing the file system. When using these features, relative paths are exposed, requiring user intervention to select files/folders.

Ethical code for developers

Protect and respect user consent - the golden rule for developers. Transparency is key when accessing user files and data. Clear permission options nurture trust and elevate user security.

Paving the way for future compatibility

API prophecy fulfillment

Though the webkitdirectory attribute lacks official standardization, implementing it along with the directory attribute is wise for forward compatibility.

<input type="file" webkitdirectory directory>

It's like future-proofing your code for evolving standards. Moreover, the multiple attribute ensures functionality across various browsers.

Browser quirks and workarounds

Every browser has its own character or "mood," if you will. So, it's vital to provide fallback mechanisms, such as using the multiple attribute, establishing a level of functionality across browsers, and facilitating multiple file selection when directory selection fails.