I have a Node Webkit Desktop App and need to download files from the server and save locally for when users are offline. I can download and save a file when I know what the file name is, but how do I read the contents of a directory on the server so I can download each file?
function cacheFiles(filelink, filepath, cb) {
var path_array = filelink.split("/");
var foldername = path_array[path_array.length - 2]
//create new folder for locally html files
var newdir = filepath + '/' + foldername;
if (fs.existsSync(newdir)){
alert('file already exists, cannot cache this file.');
} else {
fs.mkdirSync(newdir);
}
//download and save index.html - THIS WORKS
var indexfile = fs.createWriteStream(newdir+'/index.html');
var request = http.get(filelink, function(response) {
response.pipe(indexfile);
indexfile.on('finish', function() {
indexfile.close(cb);
});
});
//read contents of data folder - THIS DOESN'T WORK
var datafolder = filelink.replace('index.html','');
fs.readdir( datafolder, function (err, datafiles) {
if (!err) {
console.log(datafiles);
}else{
console.log(err) ;
}
});
}
The error I get in my console is:
"ENOENT: no such file or directory, scandir 'C:\Users\my.name\desktopApp\app\http:\www.mysite.co.uk\wp-content\uploads\wifi_corp2\data'"
The above is looking for the files locally and not at the online link I supplied in filelink eg. http://www.mysite.co.uk/wp-content/uploads/wifi_corp2/data