csv - Can I use Javascript to get a file directory list? - Stack Overflow

I'm using client-side Javascript and want to get a list of all the files in a folder that I believ

I'm using client-side Javascript and want to get a list of all the files in a folder that I believe is hosted on the same server as my .html file. I'm very unfamiliar with the terminology so I apologize in advance if I'm inaccurate or just plain wrong.

I currently use d3.text("js/data/nodes#.csv", "text/csv", someFunction) to load in a data file to work with. I figure that since all the filenames I want are templated the same way, I can hack a solution by looping through all possible numbers and only get the filenames of valid calls, like so:

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 rubbish way, I presume there must be some (much, much more) elegant method of achieving my goal. Is this possible?

I'm using client-side Javascript and want to get a list of all the files in a folder that I believe is hosted on the same server as my .html file. I'm very unfamiliar with the terminology so I apologize in advance if I'm inaccurate or just plain wrong.

I currently use d3.text("js/data/nodes#.csv", "text/csv", someFunction) to load in a data file to work with. I figure that since all the filenames I want are templated the same way, I can hack a solution by looping through all possible numbers and only get the filenames of valid calls, like so:

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 rubbish way, I presume there must be some (much, much more) elegant method of achieving my goal. Is this possible?

Share Improve this question asked Oct 1, 2014 at 20:45 Null-UsernameNull-Username 5394 silver badges19 bronze badges 1
  • Not out of the box! Remember that you are making an HTTP request to your server which implements an endpoint, in this case serving a static file. The server you have must implement a directory listing endpoint if you want a directory list. What's the server software or your server? Apache, Express(nodejs),...??? – cbayram Commented Oct 1, 2014 at 20:52
Add a ment  | 

1 Answer 1

Reset to default 3

By definition if you're using client side javascript, it doesn't have access to the server's folder structure. You could write a separate ajax call or something that would have a server side script (in whatever langauge) that would go through the directories and print them out to a json file that you would then be able to process with your javascript there. Something like this:

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 that script and parses through the json file

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745359612a4624299.html

相关推荐

  • csv - Can I use Javascript to get a file directory list? - Stack Overflow

    I'm using client-side Javascript and want to get a list of all the files in a folder that I believ

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信