Ftp

Ftp

FTP/SFTP client supporting the standard FTP/SFTP operations, such as listing, uploading, downloading, renaming and deletion.

var ftp = new Ftp();
ftp.hostName = "myftpserver";
ftp.protocol = "SFTP";
ftp.userName = "myusername";
ftp.password = "mypassword";
try {
  ftp.connect();
} catch (e) {
  throw "Could not connect to server.";
}
try {
  ftp.upload("/home/myusername/myfile.dat");
  console.log("Upload complete");
} catch (e) {
  throw "Error while uploading file.";
}
ftp.close();

The call to connect() is optional since upload() will auto-connect, but is done here so that any exceptions can be dealt with properly. The call to close() is usually optional since all clients are closed at the completion of a request or an event-handler invocation.

General Notes about this class:

  • Connection parameters, such as server-address and user-name, may be set in the constructor after constructor using fields.

  • Calling connect() is optional as each method will auto-connect.

  • Calling close() is recommended but is not mandatory as every client connection is closed when each JSS request is complete.

  • Methods that don't need to return a value return 'this' object so that method-calls may be chained together, e.g.

    new Ftp(host, userName, password).download(filePath).remove(filePath))

Constructor

new Ftp(hostNameopt, userNameopt, passwordopt, protocolopt, serverPortopt)

Source:
Parameters:
Name Type Attributes Description
hostName String <optional>

IP address or host-name of remote server.

userName String <optional>

User-name of account on remote server.

password String <optional>

Password of account on remote server.

protocol String <optional>

Protocol to use. May be FTP, FTPS, FTPSImplicit, SFTP or SCP.

serverPort String <optional>

Port number to connect to on the remote server.

Members

hostName :String

Description:
  • IP address or host-name of remote server.

Source:

IP address or host-name of remote server.

Type:
  • String

localDirectory :String

Description:
  • Current working directory in local virtual file-system. It's used only when uploading or downloading and the local path is not absolute.

Source:

Current working directory in local virtual file-system. It's used only when uploading or downloading and the local path is not absolute.

Type:
  • String

password :String

Description:
  • Password of account on remote server.

Source:

Password of account on remote server.

Type:
  • String

privateKeyFile :String

Description:
  • Virtual file-system path of file containing private key.

Source:

Virtual file-system path of file containing private key.

Type:
  • String

privateKeyPassword :String

Description:
  • Password of file containing private key.

Source:

Password of file containing private key.

Type:
  • String

protocol :String

Description:
  • Protocol to use. May be FTP, FTPS, FTPSImplicit, SFTP or SCP.

Source:

Protocol to use. May be FTP, FTPS, FTPSImplicit, SFTP or SCP.

Type:
  • String

serverDirectory :String

Description:
  • Current working directory on remote server. Before connection the value of this field is null unless it's set. If it's set before connection then its value will be the initial working directory. After connection its value will reflect the remote working directory.

Source:

Current working directory on remote server. Before connection the value of this field is null unless it's set. If it's set before connection then its value will be the initial working directory. After connection its value will reflect the remote working directory.

Type:
  • String

serverPort :Number

Description:
  • Port number to connect to on the remote server.

Source:

Port number to connect to on the remote server.

Type:
  • Number

userName :String

Description:
  • User-name of account on remote server.

Source:

User-name of account on remote server.

Type:
  • String

Methods

close(waitForServerAckopt) → {Ftp}

Description:
  • Closes the connection. Note that calling this method is recommended but not essential since all open clients are closed at the end of each request.

Source:
Parameters:
Name Type Attributes Description
waitForServerAck Boolean <optional>

If true then the client will wait for the server to acknowledge that the connection is being close. Default is 'false'.

Returns:

A reference to 'this' object is returned so that methods may be chained together (see class description).

Type
Ftp

connect() → {Ftp}

Description:
  • Connect to remote server. Note that calling this method is optional as the client will automatically connect when any other method is called if it hasn't already connected. Calling it explicitly may be useful for error-handling.

Source:
Returns:

A reference to 'this' object is returned so that methods may be chained together (see class description).

Type
Ftp

createFolder(name) → {Ftp}

Description:
  • Creates a folder with the given name under the current working directory

Source:
Parameters:
Name Type Description
name String

Name of directory to create.

Returns:

A reference to 'this' object is returned so that methods may be chained together (see class description).

Type
Ftp

download(remotePath, localPathopt) → {Ftp}

Description:
  • Downloads a file from the remote server to the local virtual file-system.

Source:
Parameters:
Name Type Attributes Description
remotePath String

Path or name of the file to be downloaded from the remote file-system.

localPath String <optional>

Path of the file being downloaded to the local virtual file-system.

Returns:

A reference to 'this' object is returned so that methods may be chained together (see class description).

Type
Ftp

exists(remotePath) → {String}

Description:
  • Tests whether or not a file or directory exists on the remote server. Note that this method doesn't strictly return a boolean. It will return false if the file/directory doesn't exist, but if the it does exist then the type ("file" or "directory") is returned. These are truthy values so the return values may be used in boolean expressions.

Source:
Parameters:
Name Type Description
remotePath String

Path or name of the file or directory in the remote file-system.

Returns:

Returns "file" or "directory" if either of these exists and false otherwise.

Type
String

getAdvancedProperty(propertyName)

Description:
  • Get the value of a SecureFTPConnection property. The JSS Ftp class is implemented by EnterpriseDT's secure FTP .NET component, SecureFTPConnection. This class is very mature and has many properties that are not exposed in the JSS Ftp class. Use this method to get the value of these properties.

Source:
Parameters:
Name Type Description
propertyName String

Name of property to get.

list(remotePathopt) → {Array.<FileInfo>}

Description:
  • Returns a listing of the remote directory in the form of an array of FileInfo objects.

Source:
Parameters:
Name Type Attributes Description
remotePath String <optional>

Path or name of the directory to be listed in the remote file-system.

Returns:

Array of FileInfo objects each representing a file or a folder.

Type
Array.<FileInfo>

listNames(remotePathopt) → {Array.<String>}

Description:
  • Returns a listing of the remote directory in the form of an array of strings being the names of the files and directories (without paths).

Source:
Parameters:
Name Type Attributes Description
remotePath String <optional>

Path or name of the directory to be listed in the remote file-system.

Returns:

Array of strings being the names of the files and directories (without paths).

Type
Array.<String>

move(fromRemotePath, toRemotePath) → {Ftp}

Description:
  • Moves or renames the given file or directory from one path or name to another. Some servers support renaming but not moving.

Source:
Parameters:
Name Type Description
fromRemotePath String

Path or name of the existing file or directory to moved in the remote file-system.

toRemotePath String

Path or name of the file or directory to move it to in the remote file-system.

Returns:

A reference to 'this' object is returned so that methods may be chained together (see class description).

Type
Ftp

remove(remotePath, recursiveopt) → {Ftp}

Description:
  • Deletes the given file or directory.

Source:
Parameters:
Name Type Attributes Default Description
remotePath String

Path or name of the file or directory to be deleted from the remote file-system.

recursive String <optional>
false

Remove all files and subdirectories of the directory as well.

Returns:

A reference to 'this' object is returned so that methods may be chained together (see class description).

Type
Ftp

setAdvancedProperty(propertyName, propertyValue)

Description:
  • Set a SecureFTPConnection property. The JSS Ftp class is implemented by EnterpriseDT's secure FTP .NET component, SecureFTPConnection. This class is very mature and has many properties that are not exposed in the JSS Ftp class. Use this method to set these properties. Properties that have non-primitive types must be initialized using .NET objects, which can be instantiated in JSS by specifying their full name, e.g. new System.Text.StringBuilder().

Source:
Parameters:
Name Type Description
propertyName String

Name of property to set.

propertyValue Object

Value to set property to.

upload(localPath, remotePathopt) → {Ftp}

Description:
  • Uploads a file from the local virtual file-system to the remote server.

Source:
Parameters:
Name Type Attributes Description
localPath String

Path of the file to be uploaded from the local virtual file-system.

remotePath String <optional>

Path or name of the file being uploaded to the remote file-system.

Returns:

A reference to 'this' object is returned so that methods may be chained together (see class description).

Type
Ftp