Class FTPClient2() Foundation

Class for network file transfer using FTP/FTPS.

Description

FTP (File Transfer Protocol) and FTPS (FTP over SSL/TLS) are standard network protocols used to transfer files between a client and server on a computer network. Instances of the FTPClient2 class enable applications to transfer files to and from FTP/FTPS servers.

A typical usage flow starts with creating an FTPClient2 instance using the :new() method, providing the host address, username, and optional proxy settings. It is important to note that the host address must be specified as a URL containing the proper scheme (for example, ftp://server.com or ftps://server.com). A runtime error is raised otherwise.

Before establishing the connection, security options can be configured using :setOption(), for example, to enable or disable TLS encryption, or to ignore certificate errors during development.

The connection is established by calling :connect(). Once connected, files and their content can be transferred using :put() and :get() and the :putFile() and :getFile() methods, respectively.

Directory operations are handled through the method :curDir() and :directory().

All operations are executed with the access rights and permissions of the user account that was authenticated when establishing the connection, ensuring that only actions permitted to that user can be performed on the server.

If an operation fails, the error details can be retrieved using the :getLastError() and :getLastMessage() methods.

Common error situations when using the FtpClient class include network connectivity issues or timeouts that prevent communication with the FTP/FTPS server, invalid login credentials or expired accounts, and insufficient permissions of the authenticated user for performing file or directory operations. Errors may also arise from failed TLS/SSL negotiation when using FTPS, attempts to access or modify directories that do not exist, invalid path specifications, or operations such as removing a non-empty directory or deleting system-protected resources.

Logging and Tracing

The File Transfer asset includes comprehensive logging capabilities that help track operations, diagnose issues, and monitor file transfer activities using the XppRtFileLogger() logging facility. The logging system always captures errors and warnings. Additional logging for different implementation layers can be enabled through the environment variable "xpp.logger". The variable accepts a semicolon-separated list of the following values:

- "ftpclient2": Log messages from the FTP protocol implementation layer

- "ftpclient2.verbose": Enable verbose logging

Constructors
:new()
Initialize a new FTP client instance.
Methods
:connect()
Establishes a connection.
:createDir()
Creates a directory on the FTP server.
:curDir()
Gets or sets the current working directory.
:delete()
Deletes a file.
:directory()
Lists files and directories.
:disconnect()
Closes the connection.
:get()
Downloads file content.
:getFile()
Downloads a file.
:getLastError()
Gets the error code of the last operation.
:getLastMessage()
Gets the error message of the last operation.
:put()
Uploads file content.
:putFile()
Uploads a local file.
:removeDir()
Removes a directory.
:setOption()
Sets client options.
:setTimeout()
Sets timeout values.
:setTransferMode()
Sets the data transfer mode.
Examples
Basic usage example
PROCEDURE Main() 
LOCAL oFTPClient2, aDir 

// Start the file logger. This is required if logging is enabled 
// via the "xpp.logger" environment variable 
XppRtFileLogger():startup() 

// Initialize FTPClient2 instance and ignore errors while server authorization 
oFTPClient2 := FTPClient2():new( "ftp://example.com", "username", "password" ) 

// Ignore invalid server certificate 
oFTPClient2:setOption( "ignore-cert-error", "ignore-all" ) 

// Connect to the remote FTP server 
oFTPClient2:connect() 
? oFTPClient2:getLastError(), oFTPClient2:getLastMessage() 

// Get a list of the files in the current directory 
aDir := oFTPClient2:directory() 
? oFTPClient2:getLastError(), oFTPClient2:getLastMessage() 
AEval( aDir, {|e| QOut(e:Name) } ) 

// Disconnect to clean up resources 
oFTPClient2:disconnect() 

RETURN 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.