admin管理员组文章数量:1401681
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.
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
Share Improve this question edited Oct 10, 2016 at 10:18 LeeTee asked Oct 7, 2016 at 13:59 LeeTeeLeeTee 6,60118 gold badges85 silver badges142 bronze badges 8- Where is the directory you are trying to download? – Martin Gottweis Commented Oct 7, 2016 at 14:00
- on my server eg. http:\www.mysite.co.uk\wp-content\uploads\wifi_corp2-1475754605\data – LeeTee Commented Oct 7, 2016 at 14:02
- Can you access the list of files, the list of filepaths to download? – Martin Gottweis Commented Oct 7, 2016 at 14:04
- request http folder will output an html file. Type your folder path in your browser and parse this html file. (depend on your server configuration) – Steeve Pitis Commented Oct 7, 2016 at 14:05
- 2 I didn't downvote, but it is likely because the question is a bit hard to understand, and it doesn't seem easy to track down where the issue might be ing from. – John Weisz Commented Oct 7, 2016 at 15:57
3 Answers
Reset to default 2 +25The following code doesn't read a remote file system, it's used for reading files on your local hard drive.
import fs from 'fs'
import path from 'path'
fs.readdir(path.resolve(__dirname, '..', 'public'), 'utf8', (err, files) => {
files.forEach((file) => console.info(file))
})
Will print out all the file names from one directory up and in a 'public' directory from the script location. You can use fs.readFile
to read the contents of each file. If they are JSON, you may read them as utf8 strings and parse them with JSON.parse
.
To read files from a remote server, they must be served via express or some other static file server:
import express from 'express'
const app = express()
app.use(express.static('public'))
app.listen(8000)
Then on the client end you could use fetch or request http library to call the express endpoint hosted at port 8000 (in this simple example.
You are mixing up the server code with the desktop app code. Obviously the desktop app can't do a readdir on your server fiiles. Just install a backup or download plugin on Wordpress.
OK, Im thinking the best way around this is to use Ajax to call a PHP function on the server to read the contents of the file.
本文标签: javascriptNodejsread and download all files in directory from server and save locallyStack Overflow
版权声明:本文标题:javascript - Node.js - read and download all files in directory from server and save locally - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744256133a2597490.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论