Directory Chooser in HTML page
Can't be done in pure HTML/JavaScript for security reasons.
Selecting a file for upload is the best you can do, and even then you won't get its full original path in modern browsers.
You may be able to put something together using Java or Flash (e.g. using SWFUpload as a basis), but it's a lot of work and brings additional compatibility issues.
Another thought would be opening an iframe
showing the user's C:
drive (or whatever) but even if that's possible nowadays (could be blocked for security reasons, haven't tried in a long time) it will be impossible for your web site to communicate with the iframe (again for security reasons).
What do you need this for?
Folder Choose in Html
I hope this can help you
you can see the folder name in alert when select a file
<script type="text/javascript"> function getfolder(e) { var files = e.target.files; var path = files[0].webkitRelativePath; var Folder = path.split("/"); alert(Folder[0]); }</script>
<input type="file" id="flup" onchange="getfolder(event)" webkitdirectory mozdirectory msdirectory odirectory directory multiple />
How to get full path of selected file on change of input type=‘file’ using javascript, jquery-ajax?
For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath
property, but if you try to get the value it returns an empty string:
$('input[type=file]').change(function () {
console.log(this.files[0].mozFullPath);
});
https://jsfiddle.net/SCK5A/
So don't waste your time.
edit: If you need the file's path for reading a file you can use the FileReader API instead. Here is a related question on SO: Preview an image before it is uploaded.
HTML multi-file input to get directory (Electon app)
The highest hierarchy you can get via this input and JavaScript is the top-most folder-name within the relative path – the one the user has chosen.
In your given example, this is assumed to be foo
or bar
.
⚠️ You will not get the absolute path (because this information is hidden, also for privacy/security reasons).
How to get path segments
Use string property webkitRelativePath
and split-of the substring until first occurrence of path-separator /
.
See also
- Get folder name on selection of folder from input field, already answered question
- MDN reference documentation
webkitdirectory
input with example
Related Topics
Responsive Separator For Horizontal List
Javaserver Faces 2.2 and Html5 Support, Why Is Xhtml Still Being Used
How to Update Non-Jsf Components (Plain Html) With Jsf Ajax
Drawing Text to <Canvas> with @Font-Face Does Not Work at the First Time
CSS - Make Divs Align Horizontally
Using Display Inline-Block Columns Move Down
How to Generate an HTML Report for Junit Results
Is Html5 Localstorage Asynchronous
Css Set Background-Image by Data-Image Attr
Accessing Object in Iframe Using Vba
Setting Transform-Origin on Svg Group Not Working in Firefox
Is <Input> Well Formed Without a <Form>
How to Determine Height of Content-Box of a Block and Inline Element