How to get the contents of the directory from local PC in javascript/jQuery?
For example from C:\Images
js, there are two built-in ways to get the current directory. You can use the __dirname variable or the process. cwd() method to get the current folder.
Web browsers (and JavaScript) can only access local files with user permission. To standardize the file access from the browser, the W3C published the HTML5 File API in 2014. It defines how to access and upload local files with file objects in web applications.
No, Javascript doesn't have access to the filesystem.
The fs. readdir() method is used to asynchronously read the contents of a given directory. The callback of this method returns an array of all the file names in the directory. The options argument can be used to change the format in which the files are returned from the method.
This only works in google chrome:
<input type="file" webkitdirectory>
This will prompt the user to select a directory and you can then access the files
property of the input to see the contained files.
It is then possible to use the File System API to construct a virtual, sandboxed file system of the user selected files and have programmatic access to this virtual filesystem as if it was a real filesystem accessed by desktop app.
There is no way otherwise because that would be a big security issue
Working demo in google chrome: http://jsfiddle.net/JwgqC/
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