Skip to main content

FTP Client

Upload and download files, manage remote folders.

Introduction​

Netuno has integrated into its low-code framework an FTP client, which allows connecting to servers, sending and receiving files, modifying folders, among other remote operations.

It supports multiple simultaneous connections, and also FTPS.

This is thanks to the Java and open-source implementation Apache Commons Net, which allows Netuno to provide these functionalities in all script languages through the _ftp resource.

More about Netuno's FTP resource.

Configuration​

The first step is to configure the FTP access credentials that Netuno will use to establish the connection.

Navigate within the application you are developing to the environment configuration file. In this example, we will use the development environment file located at:

  • config/_development.json

Check whether the FTP configurations are already present; if they do not exist yet, then at the end of the configuration file (in JSON format) add the FTP configuration parameters:

config/_development.json
{
...
"ftp": {
"default": {
"enabled": true,
"host": "my.server.net",
"port": 21,
"username": "temp",
"password": "$eCr37"
}
},
...
}

After saving the changes, simply restart Netuno to apply the configuration changes. These are the default settings for the connection, as indicated by the name default.

You can add more FTP connection settings, but only one can be defined as default.

To add more FTP connection settings, simply repeat the default configuration block and change the name default to another name of your choice, see the example:

config/_development.json
{
...
"ftp": {
"default": {
"enabled": true,
"host": "my.server.net",
"port": 22,
"username": "temp",
"password": "$eCr37"
},
"other": {
"enabled": true,
"host": "my.other-server.net",
"port": 22,
"username": "temp",
"password": "$eCr37"
}
},
...
}

A configuration similar to the example above will allow you to use the default account when it is invoked as follows in the code:

const ftp = _ftp.init()

And to use the other configuration, simply add the configuration name as a parameter to the _ftp.init() function as follows:

const ftpOther = _ftp.init("other")

If you do not add a configuration name as a parameter, the default account will always be used.

Connection​

Before doing anything, you need to start the FTP connection:

ftp.connect()

Working Directory​

When performing operations on FTP, you need to define which folder will be used.

By defining the working directory, we will be able to perform the various manipulations from this base folder.

To change the working directory:

ftp.changeWorkingDirectory("my/base/folder")

Now any operation will be performed in or from this folder.

Creating and Deleting Folders​

To create a folder, simply indicate the path of this folder:

ftp.createDirectory("temporary-folder")
ftp.createDirectory("temporary-folder/sub-folder")

To delete or remove folders:

ftp.deleteDirectory("temporary-folder/sub-folder")
ftp.deleteDirectory("temporary-folder")

Rename​

It is possible to rename files or folders; the old path is passed followed by the new path:

ftp.rename("old-name", "new-name")

Uploading Files​

To submit a text file:

ftp.uploadText("my-file.txt", "Hello FTP! This is my content...")

Receiving Files​

To get a text file:

const fileContent = ftp.downloadText("my-file.txt")
_out.println(fileContent)

Listing Folder Items​

Lists all the content of a folder:

for (const i of ftp.list("path/folder")) {
if (i.isDirectory()) {
_out.print("Folder: ")
} else if (i.isFile()) {
_out.print("File: ")
} else {
_out.print("Other: ")
}
_out.println(i.getName())
_out.println("<br/>")
}

Returns a list of objects of type FTPFile.