79 lines
2.3 KiB
JavaScript
79 lines
2.3 KiB
JavaScript
// Retrieve a file in the remote server. When the file has been retrieved,
|
|
// the callback will be called with `data` being the Buffer with the
|
|
// contents of the file.
|
|
|
|
// Store the file in `remotePath` locally in `"/Users/sergi/file.txt"`.
|
|
var remotePath = "/folder/file.txt";
|
|
var localPath = "/Users/sergi/file.txt";
|
|
ftp.get(remotePath, localPath, function(err, data) {
|
|
if (err)
|
|
return console.error(err);
|
|
|
|
console.log(remotePath + " stored successfully at " + localPath);
|
|
});
|
|
|
|
|
|
// Store the file in `remotePath` locally in `"/Users/sergi/file.txt"`.
|
|
ftp.get(remotePath, function(err, socket) {
|
|
if (err)
|
|
console.error("Something went wrong.");
|
|
|
|
var writeStream = fs.createWriteStream("/Users/sergi/file.txt");
|
|
socket.pipe(writeStream);
|
|
socket.resume();
|
|
});
|
|
|
|
// Create a directory
|
|
ftp.raw.mkd("/example_dir", function(err, data) {
|
|
if (err) return console.error(err);
|
|
|
|
console.log(data.text);
|
|
});
|
|
|
|
// Delete a directory
|
|
ftp.raw.rmd("/example_dir", function(err, data) {
|
|
if (err) return console.error(err);
|
|
|
|
console.log(data.text);
|
|
});
|
|
|
|
// Listing a directory
|
|
ftp.ls("/example_dir", function(err, files) {
|
|
if (err) return console.error(err);
|
|
console.log(files); // Contains an array of file objects
|
|
});
|
|
|
|
// Retrieving a file using streams
|
|
ftp.getGetSocket("/test_dir/testfile.txt", function(err, socket) {
|
|
if (err) return console.error(err);
|
|
|
|
var pieces = [];
|
|
// `readable` is a stream, so we can attach events to it now
|
|
socket.on("data", function(p) { pieces.push(p); });
|
|
socket.on("close", function(err) {
|
|
if (err) return console.error(new Error("readable connection error"));
|
|
|
|
// `Ftp._concat` is an internal method used to concatenate buffers, it
|
|
// is used here only for illustration purposes.
|
|
console.log(Ftp._concat(pieces)); // print the contents of the file
|
|
});
|
|
|
|
// The readable stream is already paused, we have to resume it so it can
|
|
// start streaming.
|
|
readable.resume();
|
|
});
|
|
|
|
// Storing a file in the FTP server, using streams
|
|
var originalData = Fs.createReadStream("sourceFile.txt")
|
|
)
|
|
;
|
|
originalData.pause();
|
|
|
|
ftp.getPutSocket("/remote_folder/sourceFileCopy.txt"), function(err, socket) {
|
|
if (err) return console.error(err);
|
|
originalData.pipe(socket); // Transfer from source to the remote file
|
|
originalData.resume();
|
|
}
|
|
)
|
|
;
|