Skip to content

ftp.get method

SaltwaterC edited this page Feb 3, 2013 · 2 revisions

Basic usage

Reference

ftp.get(options, [file], callback);

  • options - the Options object
  • file - optional - the file path to save the remote file, if unspecified, the response is buffered
  • callback - the completion callback which has a couple of arguments:
  • error - an error, as indicated by the Error handling cases
  • result - either the file path if the file argument is passed, or the buffered response

Shorthand syntax

var ftp = require('ftp-get');

ftp.get('ftp://localhost/foo.pdf', '/path/to/foo.pdf', function (error, result) {
	if (error) {
		console.error(error);
	} else {
		console.log('File downloaded at: ' + result);
	}
});

If you need to use authentication, pass the user:pass information to the FTP URL itself. Otherwise, it tries anonymous authentication. The target file path may be relative. path.resolve() is used to obtain the absolute path. The absolute path is also returned into the result argument if there aren't any errors.

Buffering responses

ftp.get('ftp://localhost/foo.xml', function (error, result) {
	if (error) {
		console.error(error);
	} else {
		console.log('The XML document contents: ' + result);
	}
});

Basically you need to pass the callback as the second argument of the get function instead of passing the file path. By default it uses a String buffer, but this option is deprecated in v0.3. The Buffer buffer is going to be the only supported option. However, in order to enable it, you must not use the shorthand syntax till the migration is done.

ftp.get({
	url: 'ftp://localhost/foo.xml',
	bufferType: 'buffer'
}, function (error, result) {
	if (error) {
		console.error(error);
	} else {
		console.log('The XML document contents: ' + result.toString());
	}
});
Clone this wiki locally