Reputation: 73
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 />
Upvotes: 1
Views: 3002
Reputation: 73
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.
Upvotes: 1
Reputation: 773
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.
Upvotes: 1