The %Net.FtpSession class provides a way of interacting with a FTP server so you can
send/receive files, get a list of the files on the server, rename files, delete
All the methods will return a boolean that is true if the
method succeeded and false if it failed. They will also set the two properties
ReturnCode and ReturnMessage with information
from the ftp server you are connected to. This often contains useful information if
a method fails. You should at the very least check the return value from each of the
methods after every call.
Once you have created an object of this class you need to login to the server
you wish to communicate with before you can do anything else, this is done with the
Connect() method. You can tell if you are connected to a server by
looking at the property Connected.
If an ftp server at 'TestMachine' had a binary file called 'test.exe' in the root
ftp directory then the following example will pull this file into Cache.
If 'ftp.Connect("TestMachine","ftp","firstname.lastname@example.org") Write "Not connected",! Quit
Write "Ftp server messsage:",!,ftp.ReturnMessage,!
If 'ftp.Binary() Write "Can not swap to binary mode",! Quit
Write "Mode now: ",ftp.Type,!
If 'ftp.Retrieve("test.exe",stream) Write "Failed to get file",! Quit
Write "Length of file received: ",stream.Size,!
If 'ftp.Logout() Write "Failed to logout",!
property AutoDetectPrivate as %Integer [ InitialExpression = 1 ];
When using PASV mode (see UsePASV) the remote server supplies the
IP address and port to connect to. On misconfigured servers it is possible this may report
a private IP address when we are connecting to it from a public IP address so the PASV
connection fails. We automatically detect this and use the initial IP address we had connected
to in this case, but if you set this property =0 it turns this detection off.
If this property is 2 then we never use the PASV supplied server IP and always use the original server
The Callback property is designed to allow user code in the class
%Net.FtpCallback to be called at regular intervals during
an ftp Store() or Retrieve(). This can
display the progress of the ftp operation to the user and could allow
the user to abort the transfer.
The translate table to use for the command channel, specifically for the filename/pathnames.
Normally this should not be specified in which case if the ftp server supports UTF-8 then
we will use that for the filename/pathnames, if the server does not support UTF-8 then we
will use RAW mode and just read the bytes as sent.
property LegacySSL as %Boolean [ InitialExpression = 0 ];
If true and you specify a SSLConfiguration then this class
will use non-standard implied SSL on the data and command channel rather than
using RFC4217. Depending on the configuration of the server you are talking to
it may be needed to also send 'PBSZ 0' and 'PROT P' before you can communicate, this
can be done with 'Set rc=ftp.sendCommand("PBSZ 0"),rc2=ftp.sendCommand("PROT P")'.
property ReturnCode as %Integer [ InitialExpression = 0 ];
ReturnCode is a the three digit number that the ftp server reponds to commands
with. This can be used to determine if the command completed or if there
were problems. See the rfc on ftp for more information on these codes.
ReturnMessage is set to the text message
that the ftp server responds with, this often contains useful information if
a method failed, or useful information such as the text banner you get when
you first login to an ftp server.
property SSLCheckServerIdentity as %Boolean [ InitialExpression = 0 ];
When making an SSL connection check the server identity in the server certificate matches the name of the system we are connecting to.
This defaults to being off and matches based on the rules layed out in section 3.1 of RFC 2818.
The name of the activated TLS configuration to use for ftp requests.
If specified then we use TLS on the FTP connection as specified in RFC4217.
Both the data and the command channel will be secured with TLS after the initial
connect on the command channel tells the remote server to switch to TLS mode.
property UseExtensions as %Boolean [ InitialExpression = 0 ];
Indicates whether to use FTP Extensions for IPv6 and NATs. When set, the extension commands EPRT and EPSV
will be used in place of the PORT and PASV commands. The default value is 0,
but UseExtentions is automatically set to 1 when an IPV6 address is used.
The FTP Extension commmands are useful to avoid problems using FTPS with
Network Address Translation (NAT) as when traversing firewalls.
property UsePASV as %Boolean [ InitialExpression = 1 ];
Ftp connections are formed from two TCP/IP connections to the remote server,
a command channel where the ftp commands are sent down and command responses
are retrieved and a data channel for streaming large pieces of data. The way the
data channel is connected is determined by this property. In PASV mode, the default,
this ftp client asks the server where to connect for the data channel and it then
initiates this connection to the remote server. If PASV mode is not used then the
client tells the remote server where to connect to and the remote server initiates
the data connection to this client machine. PASV mode is turned on by default because
when going through a firewall having the remote ftp server initiate the data channel
often does not work, but PASV mode will work in this case.
Switch the ftp server transfer type to Ascii. This will for example convert Cr/Lf
to Lf for Unix systems. When transfering text files you should use this mode. The
current mode can be found by looking at the property Type.
Switch the ftp server transfer type to Binary. This will store the data in exactly
the same format it is sent in. When transfering any binary files you should use
this mode. The current mode can be found by looking at the property Type.
Connect to an Ftp server. You should supply the server IP address or domain name
to connect to as the Server parameter. Also most Ftp server will require
a Username and a Password in order to allow you to login. To login to
an anonymous Ftp server use the Username="anonymous" and the Password is your email address. Port is an optional parameter that specifies the IP port number to connect
on if it is not the standard port of 21.
Read in the files that match the Pattern in a human readable format
into Stream. The Pattern can include a path as well pattern to
search for, and if no pattern is specified then it will list all the files
in this directory. The information returned contains server information like the file
size, permissions modification time as well as the filename. The format of this is
Create a new directory on the Ftp server. Path should be passed by
reference and it will return the name of the directory created. The Ftp server
will translate the path you give it into its own format (which may be different)
and is the value returned by in Path.
Given a Path this will return an array of filenames including their path in the parameter
FileArray, this parameter should be passed by reference and if not already
created it will create a new %ArrayOfDataTypes. An example of its
use assuming is:
If 'ftp.NameList("",.fileArray) Write "Failed to get name list",!
Write "List of Files:",!
For Write fileArray.GetNext(.key),! Quit:(key="")
If a Retrieve() failed because the connection was lost this allows
you to retry getting the file. So if you have got 1/2 of the original file in
the first attempt for Filename you pass the Stream with this half
into this method and it will start where the other transfer left off.
Upload the files in Directory matching the set of Wildcards to the Server.
Multiple Wildcards can be passed. In this case, each wildcard must be separated by
the Delimiter. The default Delimiter is ";".
StoreFiles() ignores subdirectories.
All files will be uploaded using the current transfer mode (Type). This means
that binary and ASCII files cannot be uploaded together in a single call. If mixed file types are needed,
separate the upload into batches, for example:
If 'ftp.Ascii() Write "Can not swap to Ascii mode",! Quit
If 'ftp.StoreFiles("/myfiles","*.txt;*.csv") Write "Failed to store text files",! Quit
If 'ftp.Binary() Write "Failed to swap to Binary mode",! Quit
If 'ftp.StoreFiles("/myfiles","*.bin") Write "Failed to store binary files",! Quit