informat.ftp FTP Client
Overview
Use the informat.ftp object to perform FTP client-related operations.
createClient
Initialize an FTP client instance
informat.ftp.createClient();Return Value Type FtpClient
FtpClient
connect
Establish a connection with the FTP server
client.connect(host, port);| Parameter | Type | Description |
|---|---|---|
| host | String | FTP server host |
| port | Integer | FTP server port |
Example
client.connect("127.0.0.1", 21);login
Login to the FTP server using username and password
client.login(username, password);| Parameter | Type | Description |
|---|---|---|
| username | String | FTP username |
| password | String | FTP password |
Return Value
Type Boolean Returns whether the login was successful
Example
client.login("ftpuser", "123456");printWorkingDirectory
Return the current working directory path
client.printWorkingDirectory();Return Value
Type String Returns the current working directory path
changeToParentDirectory
Change the current working directory to its parent directory
client.changeToParentDirectory();Return Value
Type Boolean Returns whether changing the current working directory to its parent directory was successful
changeWorkingDirectory
Change the current working directory of the FTP server
client.changeWorkingDirectory(path);| Parameter | Type | Description |
|---|---|---|
| path | String | Path to the directory |
Return Value
Type Boolean Returns whether changing the working directory was successful
Example
client.changeWorkingDirectory("/");uploadFile
Upload a file to the FTP server
client.uploadFile(localPath, remotePath);| Parameter | Type | Description |
|---|---|---|
| localPath | String | Path to the file in the app sandbox environment |
| remotePath | String | Path to the file on the FTP server |
Return Value
Type Boolean Returns whether the upload was successful
Example
client.uploadFile("demo.png", "test/demo.png");downloadFile
Download a file from the FTP server to the local app sandbox environment
client.downloadFile(remotePath, localPath);| Parameter | Type | Description |
|---|---|---|
| remotePath | String | Path to the remote file |
| localPath | String | Path to the local file in the app sandbox environment |
Return Value
Type Boolean
Returns whether the download was successful
Example
client.downloadFile("test/demo.png", "ftp/demo.png");makeDirectory
Create a folder on the FTP server
client.makeDirectory(path);| Parameter | Type | Description |
|---|---|---|
| path | String | Path to the directory |
Return Value
Type Boolean Returns whether the folder creation was successful
Example
client.makeDirectory("test");listFiles
List files and directories in the current working directory of the FTP server
client.listFiles();Return Value
Type Array<FtpFile> Returns the list of files and directories
If the return is empty and the directory contains files, you need to enable passive transfer mode
let client = null;
try {
// create connection
client = informat.ftp.createClient();
client.connect("127.0.0.1", 21);
// login
let login = client.login("ftpuser", "123456");
console.log("login:", login);
// get current path
console.log(client.printWorkingDirectory());
// enable passive transfer mode
client.getFtpClient().enterLocalPassiveMode();
// get current directory files and directories
let files = client.listFiles();
files.forEach((f) => {
console.log(f);
console.log(f.getGroup()); // file group
console.log(f.getName()); // file name
console.log(f.getSize()); // file size
console.log(f.isDirectory()); // whether it is a folder
console.log(f.isFile()); // whether it is a file
});
} finally {
if (client != null) {
// close connection
client.disconnect();
}
}disconnect
Close the connection with the FTP server
client.disconnect();Return Value
Type Boolean Returns whether the disconnection was successful
Example
Complete Example
let client = null;
try {
// create connection
client = informat.ftp.createClient();
client.connect("127.0.0.1", 21);
// login
let login = client.login("ftpuser", "123456");
console.log("login:", login);
// get current path
console.log(client.printWorkingDirectory());
// get current directory files and directories
let files = client.listFiles();
files.forEach((f) => {
console.log(f);
console.log(f.getGroup()); // file group
console.log(f.getName()); // file name
console.log(f.getSize()); // file size
console.log(f.isDirectory()); // whether it is a folder
console.log(f.isFile()); // whether it is a file
});
// create directory
client.makeDirectory("test");
// upload file
var ret = client.uploadFile("demo.png", "test/demo.png");
console.log("uploadFile ret:", ret);
// download file
ret = client.downloadFile("test/demo.png", "ftp/demo.png");
console.log("downloadFile ret:", ret);
} finally {
if (client != null) {
// close connection
client.disconnect();
}
}
