Node.js - 从服务器读取并下载目录中的所有文件并在本地保存

Lee*_*Tee 7 javascript node.js node-webkit

我有一个Node Webkit桌面应用程序,需要从服务器下载文件并在用户离线时本地保存.当我知道文件名是什么时,我可以下载并保存文件,但是如何读取服务器上目录的内容以便下载每个文件?

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) ; 
    }   
});
Run Code Online (Sandbox Code Playgroud)

}

我在控制台中遇到的错误是:

"ENOENT:没有这样的文件或目录,scandir'C:\ Users\my.name\desktopApp\app\http:\ www.mysite.co.uk\wp-content\uploads\wifi_corp2\data'"

以上是在本地查找文件而不是我在filelink中提供的在线链接, 例如. http://www.mysite.co.uk/wp-content/uploads/wifi_corp2/data

Lee*_*Tee 0

好的,我认为解决此问题的最佳方法是使用 Ajax 调用服务器上的 PHP 函数来读取文件的内容。