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
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
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.
Set ftp=##class(%Net.FtpSession).%New() If 'ftp.Connect("TestMachine","ftp","email@example.com") Write "Not connected",! Quit Write "Ftp server messsage:",!,ftp.ReturnMessage,! Set stream=##class(%GlobalCharacterStream).%New() 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",! Quit
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 address.
The Callback property is designed to allow user code in the class
%Net.FtpCallbackto be called at regular intervals during an ftp Storeor 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.
True if we are currently connected to an ftp server and false if not.
True if the connection is IPV6 protocol.
If true and you specify a
SSLConfigurationthen 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")'.
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.
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.
Server's IP address to be used in EPSV mode with IPV6 protocol.
Timeout is the amount of time to wait for a response from the ftp server before assuming that the server is not responding or the network connection is not working. The default value is 10 seconds.
The translate table to be used when reading or writing files.
Type returns the transfer mode the ftp server is currently set to. This can be either Ascii or Binary. The methods
Asciiand Binarychange the mode the server is currently set to.
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.
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.
Append the data contained in Stream to the file named in Filename.
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
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
Change the user that we are logged in as. This assumes you are already connected to the ftp server at this point.
This is a Set accessor method for the
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.
Delete the file Filename on the Ftp server.
See which features the ftp server supports
Return the current directory the Ftp server is in the parameter Path that is passed by reference.
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 server specific.
Logoff and disconnect from the Ftp server.
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:New fileArray,key If 'ftp.NameList("",.fileArray) Write "Failed to get name list",! Set key="" Write "List of Files:",! For Write fileArray.GetNext(.key),! Quit:(key="")
Delete the directory passed in Path from the Ftp server.
Rename a file from OldFilename to NewFilename.
Retrieve the file specified by Filename into Stream. If Stream is not specified it will be created, in which case you should pass it by reference so it can be returned to the caller.
Retrievefailed 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.
Set the directory on the Ftp server to Path.
Move to parent directory on the Ftp server.
Return the status of the Ftp server in the Status parameter which should be passed by reference.
Store the data in Stream in the Filename on the Ftp server.
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 ";".
StoreFilesignores 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
Return information about the type of computer you are connected to in the by reference parameter System.
This is a Get accessor method for the
Return true if this IPv4 address is a private address