1

Is it possible to list the files in a directory using only javascript? To clarify, I mean list the files on the server, not the files on the clients computer. For instance:

www.domain.com/files/

contains 4 images (.jpg)

Yogesh
  • 21
  • 5
  • for now displaying files on any local directory can do . – Yogesh Sep 23 '16 at 05:27
  • 1
    Nope. You need PHP or any server side language – Jonas Wilms Sep 23 '16 at 05:29
  • You could serve the directory as `JSON`, parse `JSON` at client-side. See also http://stackoverflow.com/questions/37634049/how-to-print-all-the-txt-files-inside-a-folder-using-java-script/ – guest271314 Sep 23 '16 at 05:43
  • Answer is yes if your server reponse your request with such information . – passion Sep 23 '16 at 05:50
  • Are you trying to read directories at local filesystem? – guest271314 Sep 23 '16 at 05:54
  • @Yogesh You can use `` element with `webkitdirectory allowdirs` attributes, `change`, `drop` events to select and drop a directory. Use `.getFilesAndDirectories()` at firefox, `.webkitGetAsEntry()`, `.createReader()` `.readEntries()` at chrome, chromium. At firefox `drop` event does not list selection as a `Directory`, but a `File`. At directories containing both files and directories, directories are read first. It is not possible to determine how many directories will be selected, use recursion, `Promise` to perform tasks when all directories and files have been read. – guest271314 Sep 23 '16 at 07:41

2 Answers2

0

What you asked for:

fs.readdirSync("./")

For example, in Express:

router.get("/someFiles", function(){
    return "<ul>"
        + fs.readdirSync("/path/to/someFiles")
            .map(function(fName){
                return "<li>"+fName+"</li>";
            })
        +"</ul>"
    ;
});

What I understand you are searching for:

bitifet
  • 3,514
  • 15
  • 37
  • No .I just want to display names of all folder present in my C drive – Yogesh Sep 23 '16 at 05:46
  • @Yogesh _"To clarify, I mean list the files on the server, not the files on the clients computer."_ , _"No .I just want to display names of all folder present in my C drive"_ ? – guest271314 Sep 23 '16 at 05:57
0

No, it isn't possible to get the local directories of the client for obvious security reasons. If you want to receive a file, then use a file input field.

Neither is possible to get the server files natively, because JavaScript is a client side language.

Bálint
  • 4,009
  • 2
  • 16
  • 27