Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the contents of the directory from local PC in javascript

How to get the contents of the directory from local PC in javascript/jQuery? For example from C:\Images

like image 919
Marcin Kostrzewa Avatar asked Nov 29 '12 11:11

Marcin Kostrzewa


People also ask

How do you access a directory in JavaScript?

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.

Can JavaScript access local files?

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.

Can you get all files from a folder path using pure JavaScript?

No, Javascript doesn't have access to the filesystem.

How do I read a directory in node JS?

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.


1 Answers

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/

like image 199
Esailija Avatar answered Nov 15 '22 17:11

Esailija