informat.sftp SFTP Client
Overview
Use informat.sftp to implement SFTP client-related functions.
createClient
Initialize SFTP client instance
informat.sftp.createClient();Return Value type is SftpClient
SftpClient
login
Login to SFTP server with username and password
client.login(username, password, host, port);| Parameter | Type | Description |
|---|---|---|
| username | String | Username |
| password | String | Password |
| host | String | SFTP server host |
| port | Integer | SFTP server port |
loginWithPrivateKey
Login to SFTP server with private key
client.loginWithPrivateKey(username, privateKey, host, port);| Parameter | Type | Description | |
|---|---|---|---|
| username | String | Username | |
| privateKey | String | Private key | |
| host | String | SFTP server host | |
| port | int | SFTP server port |
logout
Logout
client.logout();cd
Used to change the current working directory
client.cd(path);| Parameter | Type | Description |
|---|---|---|
| path | String | Path to the directory to change to |
put
Upload file to SFTP server
client.put(localPath, dst);| Parameter | Type | Description | |
|---|---|---|---|
| localPath | String | Path to the file in the app sandbox environment | |
| remotePath | String | File path on the SFTP server |
put
Upload file to SFTP server
client.put(localPath, dst, mode);| Parameter | Type | Description | |
|---|---|---|---|
| localPath | String | Path to the file in the app sandbox environment | |
| remotePath | String | File path on the SFTP server | |
| mode | int | Mode, 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
client.downloadFile(remotePath, localPath);| Parameter | Type | Description | |
|---|---|---|---|
| remotePath | String | Remote file path | |
| localPath | String | Local app sandbox environment path |
ls
List files and directories in the specified directory on the SFTP server
client.ls(path);| Parameter | Type | Description |
|---|---|---|
| path | String | Path 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
client.mkdir(path);| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the new directory to create |
pwd
Get the current working directory in the SFTP session
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.
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
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();
}
}
