0

I have the code bellow...in short it is mean't to generate a list of all files in a directory and then for every file in the list it would add a new gallery element to the page.

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <style media="screen">
      div.gallery {
        margin: 5px;
        border: 1px solid #ccc;
        float: left;
        width: 180px;
      }

      div.gallery:hover {
        border: 1px solid #777;
      }

      div.gallery img {
        width: 100%;
        height: auto;
      }

      div.desc {
        padding: 15px;
        text-align: center;
      }
    </style>
  </head>
  <body>
    <h1>Test</h1>
    <script type="text/javascript">
      var fs = require('fs');
      var files = fs.readdirSync('uploads/');

      for (item in files) {
        document.write('<div class="gallery">');
        document.write('<a target="_blank" href="uploads/'+item+'">');
        document.write('<img src="uploads/'+item+'" width="600" height="400">');
        document.write('</a>');
        document.write('<div class="desc">'+item+'</div>');
        document.write('</div>');
       }
    </script>
  </body>
</html>

From some quick research var fs = require('fs'); and var files = fs.readdirSync('uploads/'); are supposed to get the files in the directory into the list and then the for statement is mean't to add them to the page. However when I reload the page it remains blank. Does anyone have any ideas?

myname
  • 1
  • 2
  • 2
    `var fs = require('fs');` that's nodejs - you can't do that in a browser - are you trying to access files on the client or on the server? – Jaromanda X Sep 29 '19 at 13:37
  • You can't use require and FileSystem on Client Side https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined/19059825 – GTech Sep 29 '19 at 13:38
  • The `uploads/` folder in on the web server same as the `js` file. I'm trying to get a list of all the files in a directory on the server. – myname Sep 29 '19 at 23:13

0 Answers0