0

I'm trying to make a simple webpage that you can input a directory path in and it lists all the files in that directory. Is there a way I can do that directly in the .html file with js script? I have the path as a variable.

I'm very new to these programmin languages and don't know much about these.

2 Answers2

0

A webpage does not have access to a client's file system. No website can see your files.

However, if you're talking about server side javascript, that's different. Server side javascript file listing can be achieved through this:

var fs = require('fs');
var files = fs.readdirSync('/assets/photos/');

Reference: Get list of filenames in folder with Javascript

Hope that helps

DrMoney
  • 355
  • 2
  • 8
  • I've tried that, but it doesn't work: Uncaught ReferenceError: require is not defined – Ukkonoahh May 24 '20 at 17:34
  • `FS` - File saver, is only defined when it's not running as a web page. If you have an express server it will work but if you're just making an html website then it won't work. A web page cannot read a user's files – DrMoney May 25 '20 at 00:04
0

EDIT 1 (upload files with html):

You need to be more specific. But i will supose that you want to navigate into files for upload some file on your webpage:

You could do this and when you press the button you will can navigate in your files and select as you want:

<input type="file" name="" id="">

EDIT 2 (using server languages for working with files):

First step: you need a lenguage server as php or nodejs (it use internally javascript chromev8). I recomend you to use nodejs becose its easier for people who already know javascript.

If you wanna nodejs, install it: https://nodejs.org/en/ . If you wanna php you can install apache xampp that also install a version of mysql database:https://www.apachefriends.org/index.html

Second step: working with files: php https://www.php.net/manual/en/intro.dio.php nodejs: https://nodejs.org/api/fs.html#fs_file_system (you will need to know about synchronous and asynchronous programming).

  • I want to browse files in the server. – Ukkonoahh May 24 '20 at 17:53
  • Ok, you can do that, but not with html or javascript in the client side (front-end). You need to use a server languages as php or javascript with nodejs (back-end). –  May 24 '20 at 17:59
  • Guess I need to dig in deeper into those. I have no idea how they work. I'm more of a python guy :D – Ukkonoahh May 24 '20 at 18:04
  • Yes, also you can use python. To know how server lenguages work may take time, but dont worry, then it become easy. –  May 24 '20 at 18:20