Skip to content

informat.sftp SFTP Client ​

Overview ​

Use informat.sftp to implement SFTP client-related functions.


createClient ​

Initialize SFTP client instance

javascript
informat.sftp.createClient();

Return Value type is SftpClient

SftpClient ​

login ​

Login to SFTP server with username and password

js
client.login(username, password, host, port);
ParameterTypeDescription
usernameStringUsername
passwordStringPassword
hostStringSFTP server host
portIntegerSFTP server port

loginWithPrivateKey ​

Login to SFTP server with private key

js
client.loginWithPrivateKey(username, privateKey, host, port);
ParameterTypeDescription
usernameStringUsername
privateKeyStringPrivate key
hostStringSFTP server host
portintSFTP server port

logout ​

Logout

js
client.logout();

cd ​

Used to change the current working directory

js
client.cd(path);
ParameterTypeDescription
pathStringPath to the directory to change to

put ​

Upload file to SFTP server

js
client.put(localPath, dst);
ParameterTypeDescription
localPathStringPath to the file in the app sandbox environment
remotePathStringFile path on the SFTP server

put ​

Upload file to SFTP server

js
client.put(localPath, dst, mode);
ParameterTypeDescription
localPathStringPath to the file in the app sandbox environment
remotePathStringFile path on the SFTP server
modeintMode, optional values are as follows:
0: If the target file already exists, it will be overwritten. This is the default mode
1: If the transfer is interrupted, it will resume from where it left off instead of restarting
2: If the target file already exists, the uploaded data will be appended to the end of the file

downloadFile ​

Download specified file from SFTP server to local app sandbox environment

js
client.downloadFile(remotePath, localPath);
ParameterTypeDescription
remotePathStringRemote file path
localPathStringLocal app sandbox environment path

ls ​

List files and directories in the specified directory on the SFTP server

js
client.ls(path);
ParameterTypeDescription
pathStringPath to the remote directory to list contents

Return Value Type is Array<SftpFile> Returns file list

mkdir ​

Create a new directory on the SFTP server

js
client.mkdir(path);
ParameterTypeDescription
pathStringPath of the new directory to create

pwd ​

Get the current working directory in the SFTP session

js
client.pwd();

Return Value Type is String Returns the current working directory in the SFTP session

disconnect ​

End the session with the remote SSH server.

js
client.disconnect();

Critical for preventing network resource and file descriptor leaks. If this method is not called, long-running applications may accumulate unclosed connections, which can affect performance or lead to resource exhaustion.

Here is a complete example

js
var host = "192.168.1.120";
var port = 22;
var userName = "sftpuser";
var password = "123456";
var client = null;
try {
  client = informat.sftp.createClient();
  client.login(userName, password, host, port);
  console.log(client.pwd());
  client.mkdir("test/d");
  client.put("logo.jpg", "test/d/logo.jpg"); //Note: You need to upload a file to the app sandbox environment first
  client.downloadFile("test/d/logo.jpg", "sftp/logo.jpg");
} finally {
  if (client != null) {
    client.disconnect();
  }
}