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

Ben picture Ben · Oct 17, 2012 · Viewed 125.3k times · Source

So I have a basic form input with the type "file" however I want the user to be able to select a folder location and not a file.

How can I get this input to select a folder and not a file, or is there another way to do it?

Answer

bwindels picture bwindels · May 27, 2013

Stumbled on this page as well, and then found out this is possible with just javascript (no plugins like ActiveX or Flash), but just in chrome:

https://plus.google.com/+AddyOsmani/posts/Dk5UhZ6zfF3

Basically, they added support for a new attribute on the file input element "webkitdirectory". You can use it like this:

<input type="file" id="ctrl" webkitdirectory directory multiple/>

It allows you to select directories. The multiple attribute is a good fallback for browsers that support multiple file selection but not directory selection.

When you select a directory the files are available through the dom object for the control (document.getElementById('ctrl')), just like they are with the multiple attribute. The browsers adds all files in the selected directory to that list recursively.

You can already add the directory attribute as well in case this gets standardized at some point (couldn't find any info regarding that)