Can I use Javascript to get a list of file directories?

I use Javascript on the client side and I want to get a list of all the files in the folder, which, it seems to me, is located on the same server as my .html file. I am very unfamiliar with the terminology, so I apologize in advance if I am inaccurate or just wrong.

I am currently using d3.text("js/data/nodes#.csv", "text/csv", someFunction) to load the data file to work. I believe that since all the file names I want, the templates are the same, I can crack the solution by going through all possible numbers and getting only the names of the actual calls:

 function getDirList() { var possiblePathsList = something predetermined; var directoryList = []; possiblePathsList.forEach(function(path){ if (isPath(path)) { directoryList.push(path); } }); return directoryList; } function isPath(path){ d3.text(path, "text/csv", function(data){ return (data !== null); }); } 

Because I can get a list in this very trashy way, I guess there must be some kind of (much more elegant way to achieve my goal). Is it possible?

0
source share
1 answer

By definition, if you use javascript on the client side, it does not have access to the server folder structure. You could write a separate ajax call or something that would have a server side script (in any langauge) that would go through directories and print them into a json file that you could handle with your javascript. Something like that:

ajax.php:

 $directory = "temp/"; $dir = opendir($directory); $structure = array(); while($file = readdir($dir)){ $structure[] = $file; } print json_encode($structure); exit(); 

Then you will have some javascript that calls this script and parses through json file

+2
source

All Articles