Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select directory path in JavaScript [duplicate]

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)

like image 536
David Avatar asked Nov 07 '22 20:11

David


1 Answers

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.

like image 176
Boris Avatar answered Nov 15 '22 11:11

Boris