1

I tried the below code in NW.js to read in all files with full path under a given folder but not working. What's wrong? Thanks.

function chooseFiles() {
    var files = $('#fileDialog')[0].files;
    for (var i = 0; i < files.length; ++i) {
        console.log(files[i].path);
    }
}
chooseFiles('#fileDialog');
<input type="file" id="fileDialog" nwdirectory />
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Kevin H.
  • 73
  • 1
  • 8
  • Kevin, can you clarify what's "not working"? This is vague. What output do you see? What error messages? – jamesmortensen Apr 18 '15 at 08:01
  • @jmort253, thanks for your reply. When I select the folder (Choose File -> -> OK) it displays the folder name in the input dialog but it does not print out the files within this folder in the Console. I would like to get all full path of these files. Thanks. – Kevin H. Apr 18 '15 at 14:59
  • http://stackoverflow.com/a/5827895/552792 Does this help? You may need to read the files within the directory using some other method. – jamesmortensen Apr 19 '15 at 11:15
  • @jmort253, thanks for the link. It works perfectly in app.js for node.js but for NW.js in my index.html I have a script tags to do the same but couldn't get to run successfully. It seems for NW.js I have to run `npm install fs` first due to `Uncaught ReferenceError: path is not defined` but still no go. Per [link](https://github.com/nwjs/nw.js/wiki/Using-Node-modules) it should work but not. Any ideas how to use this module in NW.js? Thanks. – Kevin H. Apr 19 '15 at 18:22
  • Also, please check out this [link](https://github.com/nwjs/nw.js/wiki/File-dialogs). I have the impression that NW.js has a built-in module/fn for this? – Kevin H. Apr 19 '15 at 18:30

2 Answers2

1

I'm not sure if you're using exactly the code that you pasted here, but it doesn't appear to be doing anything when the user actually chooses something. If you were to pick something using the input then call chooseFiles() it should work. At least it did in my nw.js app I quickly set up.

If you want the files to appear in console.log() when the user completes their selection, I think you should be able to do it using the code below:

<html>
<head>
    <script src="jquery.js"></script>
    <script>
    $(function () {
        $("#fileDialog").on("change", function () {
            var files = $(this)[0].files;
            for (var i = 0; i < files.length; ++i) {
            console.log(files[i].path);
            }
        });
    });
    </script>
</head>
<body>
    <input type="file" id="fileDialog" nwdirectory />
</body>
</html>

If you want the files to be logged immediately when user selects a directory using the file dialog, I think the on("change") might be what you're looking for. It worked for me using nw.js v0.12 so give it a shot and see if that is what you're looking for.

thetoast
  • 763
  • 4
  • 9
  • Hi thetoast, thanks for your time. I am using the same nw.js version too. I tried your code with a fresh nw.js v0.12 but still not able to print the files. In console the only line it printed is the selected folder path. I also tried with two diff jquery versions (v1.9.1 and v2.1.3). Still not sure why it's not working for me. I am on Win7 if that makes any diff. Thanks. – Kevin H. Apr 23 '15 at 01:28
  • @KevinH. Does it make any difference if you use `webkitdirectory` instead of `nwdirectory`? – thetoast Apr 23 '15 at 03:44
  • `webkitdirectory` returns the dir path like `nwdirectory`. I will accept your first answer because it led me to the right direction. Thanks again mate! – Kevin H. Apr 23 '15 at 04:15
1

I misunderstood how nwdirectory works. NW doc states "nwdirectory is a bit similar to webkitdirectory because it let user select a directory too, but it will not enumerate all files under the directory but directly returns the path of directory"

To return list of files with path I just need to use multiple in input like so.

<input type="file" id="fileDialog" multiple />

I also found a working code from How to find out if reading dir is completed to read and return directories and files with pull path recursively. Thanks again to both.

Community
  • 1
  • 1
Kevin H.
  • 73
  • 1
  • 8