131

My website is serving a lot of pictures from /assets/photos/ folder. How can I get a list of the files in that folder with Javascript?

bevanb
  • 7,671
  • 9
  • 50
  • 87

10 Answers10

147

The current code will give a list of all files in a folder, assuming it's on the server side you want to list all files:

var fs = require('fs');
var files = fs.readdirSync('/assets/photos/');
Christoffer Karlsson
  • 4,025
  • 2
  • 19
  • 36
  • 13
    I meant accessing a list of files from the client, sorry wasn't more clear. – bevanb Jul 07 '15 at 16:37
  • 10
    Where does require() come from or is defined??? Your code proiduces a "'require' is not defined" error. Have you tested your code? If so, have you maybe missed some important part referring to 'require'? – Apostolos Jun 14 '18 at 09:52
  • 8
    @Apostolos As OP notes in the description, this needs to be server-side code. You can not run this in a browser. Running this script with Node.js does not throw any errors. – Naltroc Aug 24 '18 at 20:25
  • 2
    This is a good way but seems doesn't answer the question directly. The simple answer is No, in JS from client side(browser) there is no direct way to get list of files in a folder/directory on server side. – Akshay Hiremath May 06 '19 at 18:55
  • 4
    require() is only supported with nodejs – Dulmina Mar 09 '20 at 10:26
  • `Uncaught ReferenceError: require is not defined` – Friedrich Oct 18 '20 at 00:28
  • This top rated answer is one very specific to back-end, which is not what most people mean when they say javascript. – Cybernetic Oct 23 '21 at 15:45
  • @Apostolos I found this video to be helpful in understanding the basics about NodeJS: https://www.youtube.com/watch?v=pU9Q6oiQNd0 – WhyWhat May 15 '22 at 08:47
  • Thanks @WhyWhat. I hate to use Youtube for things requiring short explanations. I wonder who doesn't! Anyway, it's a 5-years-ago question ... Thanks, anyway! – Apostolos May 16 '22 at 08:51
39

No, Javascript doesn't have access to the filesystem. Server side Javascript is a whole different story but I guess you don't mean that.

Simpal Kumar
  • 3,765
  • 3
  • 26
  • 48
12

I write a file dir.php

var files = <?php $out = array();
foreach (glob('file/*.html') as $filename) {
    $p = pathinfo($filename);
    $out[] = $p['filename'];
}
echo json_encode($out); ?>;

In your script add:

<script src='dir.php'></script>

and use the files[] array

IfThenElse
  • 449
  • 5
  • 13
10

For client side files, you cannot get a list of files in a user's local directory.

If the user has provided uploaded files, you can access them via their input element.

<input type="file" name="client-file" id="get-files" multiple />


<script>
var inp = document.getElementById("get-files");
// Access and handle the files 

for (i = 0; i < inp.files.length; i++) {
    let file = inp.files[i];
    // do things with file
}
</script>
Naltroc
  • 954
  • 1
  • 14
  • 34
8

For getting the list of filenames in a specified folder, you can use:

fs.readdir(directory_path, callback_function)

This will return a list which you can parse by simple list indexing like file[0],file[1], etc.

double-beep
  • 4,567
  • 13
  • 30
  • 40
Ahmad Zafar
  • 91
  • 1
  • 3
3

I made a different route for every file in a particular directory. Therefore, going to that path meant opening that file.

function getroutes(list){
list.forEach(function(element) {
  app.get("/"+ element,  function(req, res) {
    res.sendFile(__dirname + "/public/extracted/" + element);
  });
});

I called this function passing the list of filename in the directory __dirname/public/extracted and it created a different route for each filename which I was able to render on server side.

2

I use the following (stripped-down code) in Firefox 69.0 (on Ubuntu) to read a directory and show the image as part of a digital photo frame. The page is made in HTML, CSS, and JavaScript, and it is located on the same machine where I run the browser. The images are located on the same machine as well, so there is no viewing from "outside".

var directory = <path>;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', directory, false); // false for synchronous request
xmlHttp.send(null);
var ret = xmlHttp.responseText;
var fileList = ret.split('\n');
for (i = 0; i < fileList.length; i++) {
    var fileinfo = fileList[i].split(' ');
    if (fileinfo[0] == '201:') {
        document.write(fileinfo[1] + "<br>");
        document.write('<img src=\"' + directory + fileinfo[1] + '\"/>');
    }
}

This requires the policy security.fileuri.strict_origin_policy to be disabled. This means it might not be a solution you want to use. In my case I deemed it ok.

2540625
  • 10,156
  • 8
  • 46
  • 53
2

If you use nginx, you can set autoindex on, then you request url, you can get the file names from response.

<script>
  function loadDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
      if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
      }
    };
    xhttp.open("GET", "/assets/photos/", true);
    xhttp.send();
  }

  function myFunction(xml) {
    // console.log(xml.responseText)
    var parser = new DOMParser();
    var htmlDoc = parser.parseFromString(xml.responseText, 'text/html');
    var preList = htmlDoc.getElementsByTagName("pre")[0].getElementsByTagName("a")
    for (i = 1; i < preList.length; i++) {
      console.log(preList[i].innerHTML)
    }
  }
</script>
-1

I understand that the problem is old but still comes back in many issues. It's true that on client side you can't list files in a local directory using pure JavaScript. Below is a complete snippet that uses PHP and transfers the file list with extensions to JavaScript.

<?PHP
  $out = array(); 
  $out = scandir("MT940");      // Or any path to local dir to be listed
  $out = array_diff($out, array('.', '..'));    // Skip current and parent dir 
?>

<!DOCTYPE html>
<html lang="pl-PL">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>  
  <title></title>
</head>
<body>
  <div class="service-container"        
    data-files= '<?php echo (json_encode($out)); ?>'>   
  </div>
  <script>
    let files;     
    $(document).ready(function() {
      $('.service-container').each(function() {
        let container = $(this);
          files = container.data('files');
          fileNames = Object.values(files);
      });
    });
  </script> 
  
  <!-- Some HTML code here -->
  
  <script>
    let fileNames = [];
    $(document).ready(function() {
      console.log(fileNames);
      // Do something with fileNames
    });
  </script>
</body>
</html>
AndyPL
  • 1
-2

Applying JSONP to IfTheElse answer:

In /dir.php write the following:

<?php
    $out = array();
    foreach (glob('file/*.html') as $filename) {
        $p = pathinfo($filename);
        $out[] = $p['filename'];
    }
    echo 'callback(' . json_encode($out) . ')';
?>

In your index.html add this script:

<script>
    /* this function will be fired when there are files
       in dir search in php' glob
     */
    function callback(files) {
        alert(files);
     }

    /* call inside document.ready to make sure the
       callback is already loaded
     */
    $(document).ready(function() {
        let php = document.createElement("script");
        php.setAttribute("src", "/dir.php");
        document.body.appendChild(php);
    });
</script>
Carlos Barcellos
  • 526
  • 1
  • 4
  • 9