0

I want to create a folder chooser, to select one or even better multiple folders on the server side and get their path. For this I need some kind of FolderPicker or a FolderTreeView.

I tried jQuery File Tree (www.abeautifulsite.net/jquery-file-tree/) but I only get the folder structure of my project root. Is it with fs even possible to access something outside the project folder?

I would appreciate every little information.

1 Answers1

0

Hope that it may help you.

Possible duplicate: https://stackoverflow.com/a/21459809/5228251

Below function is been taken from the ^above link.

var _getAllFilesFromFolder = function(dir) {

    var filesystem = require("fs");
    var results = [];

    filesystem.readdirSync(dir).forEach(function(file) {

        file = dir+'/'+file;
        var stat = filesystem.statSync(file);

        if (stat && stat.isDirectory()) {
            results = results.concat(_getAllFilesFromFolder(file))
        } else results.push(file);

    });

    return results;
};

usage: _getAllFilesFromFolder(__dirname + '/' + 'foldername');

^ the above function returns results as an array containing paths of every single file in that 'foldername' directory.. and parse it according to the js/jquery library (configs) you're using on client-side before render.

and then you may use any js library to display in tree structure on client-side. I have used jstree in my project few months back. You may use any other as well like jquery-fileTree or etc.

use jade / html (whatever you want) with express to render the resultant data to view.

Here is my code to convert the retrieved into tree (directory) structure format:

https://gist.github.com/narainsagar/79d742ab9c62e29a81d1b1bba804782f

Thanks.

Community
  • 1
  • 1
narainsagar
  • 1,079
  • 2
  • 13
  • 29