Is there any way to select a directory via JavaScript?
Not for uploading a file, just to choose a directory path. (Directory dialog or something)
You can't, for security reasons (you don't want a website to be able to know about your file system).
See below, when you get the value of a file input, it will be mangled (on my computer, for example it will always be c:/fakepath/something
). The file input also has problems for your usecase : selecting a folder will give you the list of its contents, so an empty folder will log nothing in my snippet.
function browseResult(e){
var fileselector = document.getElementById('fileselector');
console.log(fileselector.value);
}
<input id="fileselector" type="file" onchange="browseResult(event)" webkitdirectory directory multiple="false" style="display:none" />
<button onclick="getElementById('fileselector').click()">browse</button>
You could do it via a plugin, such as Flash, Java or Air for example, but users would have to either have it installed already or install it. The plugin ecosystem seems pretty much dead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With