Open chenweibo opened 5 years ago
Here is a basic example
var Client = require('ftp');
var fs = require('fs');
var c = new Client();
c.on('ready', function() {
c.list(function(err, list) {
if (err) throw err;
for(var i = 0; i < list.length; i++){
const name = list[i].name
c.get(name, function(err, stream) {
if (err) console.dir(err);
if(!err){
stream.once('close', function() { c.end(); });
stream.pipe(fs.createWriteStream(name,{flags: 'w'}));
}
});
}
c.end();
});
});
c.on('error', function(err){
console.log(err)
})
// connect to localhost:21 as anonymous
c.connect();
How can you list the files in a subfolder?
I didn't have to handle this case. According to the documentation, object in list have a "type" property that distinguishes whether it is a file or a folder. On the basis of this property, you should be able to adapt the treatment
Not sure if this is just because I don't understand streams well, but I'm trying to do basically the same thing as above, but save the contents of the file to an array of strings and I keep getting an error saying:
Error: Unable to make data connection
at Socket.<anonymous> (node_modules/ftp/lib/connection.js:935:10)
at Object.onceWrapper (events.js:421:28)
at Socket.emit (events.js:327:22)
at Socket.EventEmitter.emit (domain.js:485:12)
at Object.cb (node_modules/ftp/lib/connection.js:575:18)
at Parser.<anonymous> (node_modules/ftp/lib/connection.js:117:20)
at Parser.emit (events.js:315:20)
at Parser.EventEmitter.emit (domain.js:485:12)
at Parser._write (node_modules/ftp/lib/parser.js:59:10)
at doWrite (_stream_writable.js:392:12)
Here is my code, I'm not sure what could be causing this error:
c.on("ready", function() {
c.list(function(err, list) {
if (err) throw err;
for (var i = 0; i < list.length; i++) {
const name = list[i].name;
c.get(name, function(err, stream) {
if (err) console.log(err);
if (!err) {
stream.once("close", function() {
console.log("stream closing");
c.end();
});
var body = "";
stream.setEncoding("utf8");
stream.on("data", (chunk) => {
body += chunk;
});
stream.on("end", () => {
console.log("end", body);
});
}
});
}
c.end();
});
});
How can you list the files in a subfolder?
using list method. Their is only single folder.
I am getting this error after few second Error: Unable to make data connection @astr0-4 @qdevos
There are no examples to download all the files inside the entire folder, thanks.