Skip to main content

FTP Client

Introduction

Netuno integrates an FTP client into its low-code framework, allowing you to connect to servers, send and receive files, modify folders, and perform other remote operations.

It supports multiple simultaneous connections, including FTPS.

This is thanks to its Java and open-source Apache Commons Net implementation, which allows Netuno to provide these features in all scripting languages through the _ftp feature.

More about Netuno's FTP feature.

Configuration

The first step will be to configure the FTP access data 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 if it already contains FTP settings. If it doesn't already exist, add the FTP configuration parameters at the end of the configuration file (in JSON format):

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

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

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

To add more FTP connection settings, simply repeat the default configuration block and replace the name default with 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 called 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 Folder

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

By defining the working folder, we can perform various operations from this base folder.

To change the working folder:

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

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

Creating and Deleting Folders

When creating a folder, simply specify the path to 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

You can rename files or folders by passing the old path and then the new path:

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

Upload Files

To submit a text file:

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

Receiving Files

To receive a text file:

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

List Items in Folder

Lists all the contents 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.