Skip to content

informat.ftp FTP Client ​

Overview ​

Use the informat.ftp object to perform FTP client-related operations.

createClient ​

Initialize an FTP client instance

javascript
informat.ftp.createClient();

Return Value Type FtpClient

FtpClient ​

connect ​

Establish a connection with the FTP server

js
client.connect(host, port);
ParameterTypeDescription
hostStringFTP server host
portIntegerFTP server port

Example

js
client.connect("127.0.0.1", 21);

login ​

Login to the FTP server using username and password

js
client.login(username, password);
ParameterTypeDescription
usernameStringFTP username
passwordStringFTP password

Return Value

Type Boolean Returns whether the login was successful

Example

js
client.login("ftpuser", "123456");

printWorkingDirectory ​

Return the current working directory path

js
client.printWorkingDirectory();

Return Value

Type String Returns the current working directory path

changeToParentDirectory ​

Change the current working directory to its parent directory

js
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

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

Return Value

Type Boolean Returns whether changing the working directory was successful

Example

js
client.changeWorkingDirectory("/");

uploadFile ​

Upload a file to the FTP server

js
client.uploadFile(localPath, remotePath);
ParameterTypeDescription
localPathStringPath to the file in the app sandbox environment
remotePathStringPath to the file on the FTP server

Return Value

Type Boolean Returns whether the upload was successful

Example

js
client.uploadFile("demo.png", "test/demo.png");

downloadFile ​

Download a file from the FTP server to the local app sandbox environment

js
client.downloadFile(remotePath, localPath);
ParameterTypeDescription
remotePathStringPath to the remote file
localPathStringPath to the local file in the app sandbox environment

Return Value

Type Boolean
Returns whether the download was successful

Example

js
client.downloadFile("test/demo.png", "ftp/demo.png");

makeDirectory ​

Create a folder on the FTP server

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

Return Value

Type Boolean Returns whether the folder creation was successful

Example

js
client.makeDirectory("test");

listFiles ​

List files and directories in the current working directory of the FTP server

js
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

js
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

js
client.disconnect();

Return Value

Type Boolean Returns whether the disconnection was successful

Example

Complete Example

js
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();
  }
}