class %Net.FtpSession extends %Library.RegisteredObjectThe %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 files, etc.
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 InterSystems IRIS.
Set ftp=##class(%Net.FtpSession).%New() If 'ftp.Connect("TestMachine","ftp","firstname.lastname@example.org") 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
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 address.
Property methods: AutoDetectPrivateDisplayToLogical(), AutoDetectPrivateGet(), AutoDetectPrivateIsValid(), AutoDetectPrivateLogicalToDisplay(), AutoDetectPrivateNormalize(), AutoDetectPrivateSet()
property Callback as %Net.FtpCallback;
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.
Property methods: CallbackGet(), CallbackGetSwizzled(), CallbackIsValid(), CallbackNewObject(), CallbackSet()
property CommandTranslateTable as %String;
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 methods: CommandTranslateTableDisplayToLogical(), CommandTranslateTableGet(), CommandTranslateTableIsValid(), CommandTranslateTableLogicalToDisplay(), CommandTranslateTableLogicalToOdbc(), CommandTranslateTableNormalize(), CommandTranslateTableSet()
property Connected as %Boolean [ InitialExpression = 0 ];
True if we are currently connected to an ftp server and false if not.
Property methods: ConnectedDisplayToLogical(), ConnectedGet(), ConnectedIsValid(), ConnectedLogicalToDisplay(), ConnectedNormalize(), ConnectedSet()
property IsIPV6 as %Boolean [ InitialExpression = 0 ];
True if the connection is IPV6 protocol.
Property methods: IsIPV6DisplayToLogical(), IsIPV6Get(), IsIPV6IsValid(), IsIPV6LogicalToDisplay(), IsIPV6Normalize(), IsIPV6Set()
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 methods: LegacySSLDisplayToLogical(), LegacySSLGet(), LegacySSLIsValid(), LegacySSLLogicalToDisplay(), LegacySSLNormalize(), LegacySSLSet()
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.
Property methods: ReturnCodeDisplayToLogical(), ReturnCodeGet(), ReturnCodeIsValid(), ReturnCodeLogicalToDisplay(), ReturnCodeNormalize(), ReturnCodeSet()
property ReturnMessage as %String;
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 methods: ReturnMessageDisplayToLogical(), ReturnMessageGet(), ReturnMessageIsValid(), ReturnMessageLogicalToDisplay(), ReturnMessageLogicalToOdbc(), ReturnMessageNormalize(), ReturnMessageSet()
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.
Property methods: SSLCheckServerIdentityDisplayToLogical(), SSLCheckServerIdentityGet(), SSLCheckServerIdentityIsValid(), SSLCheckServerIdentityLogicalToDisplay(), SSLCheckServerIdentityNormalize(), SSLCheckServerIdentitySet()
property SSLConfiguration as %String;
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 methods: SSLConfigurationDisplayToLogical(), SSLConfigurationGet(), SSLConfigurationIsValid(), SSLConfigurationLogicalToDisplay(), SSLConfigurationLogicalToOdbc(), SSLConfigurationNormalize(), SSLConfigurationSet()
property ServerAddr as %String;
Server's IP address to be used in EPSV mode with IPV6 protocol.
Property methods: ServerAddrDisplayToLogical(), ServerAddrGet(), ServerAddrIsValid(), ServerAddrLogicalToDisplay(), ServerAddrLogicalToOdbc(), ServerAddrNormalize(), ServerAddrSet()
property Timeout as %Integer [ InitialExpression = 10 ];
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.
Property methods: TimeoutDisplayToLogical(), TimeoutGet(), TimeoutIsValid(), TimeoutLogicalToDisplay(), TimeoutNormalize(), TimeoutSet()
property TranslateTable as %String;
The translate table to be used when reading or writing files.
Property methods: TranslateTableDisplayToLogical(), TranslateTableGet(), TranslateTableIsValid(), TranslateTableLogicalToDisplay(), TranslateTableLogicalToOdbc(), TranslateTableNormalize(), TranslateTableSet()
property Type as %String [ Calculated ];
Type returns the transfer mode the ftp server is currently set to. This can be either Ascii or Binary. The methods Ascii() and Binary() change the mode the server is currently set to.
Property methods: TypeDisplayToLogical(), TypeGet(), TypeIsValid(), TypeLogicalToDisplay(), TypeLogicalToOdbc(), TypeNormalize()
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 methods: UseExtensionsDisplayToLogical(), UseExtensionsGet(), UseExtensionsIsValid(), UseExtensionsLogicalToDisplay(), UseExtensionsNormalize(), UseExtensionsSet()
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.
Property methods: UsePASVDisplayToLogical(), UsePASVGet(), UsePASVIsValid(), UsePASVLogicalToDisplay(), UsePASVNormalize(), UsePASVSet()
method Append(Filename As %String, Stream As %AbstractStream) as %Boolean [ Language = objectscript ]
Append the data contained in Stream to the file named in Filename.
method Ascii() as %Boolean [ Language = objectscript ]
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.
method Binary() as %Boolean [ Language = objectscript ]
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.
Change the user that we are logged in as. This assumes you are already connected to the ftp server at this point.
method Connect(Server As %String, Username As %String, Password As %String, Port As %Integer = 21) as %Boolean [ Language = objectscript ]
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.
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.
method Features() as %Boolean [ Language = objectscript ]
See which features the ftp server supports
classmethod GetDevice() as %String [ Language = objectscript ]
Return the current directory the Ftp server is in the parameter Path that is passed by reference.
method List(Pattern As %String, ByRef Stream As %AbstractStream) as %Boolean [ Language = objectscript ]
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.
method Logout() as %Boolean [ Language = objectscript ]
Logoff and disconnect from the Ftp server.
If the FTP server supports it then this will return a files modification time based on GMT as a float value in the Time output argument. Returns true if it completed correctly.
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.
method NameList(Path As %String, ByRef FileArray As %ArrayOfDataTypes) as %Boolean [ Language = objectscript ]
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.
method Rename(OldFilename As %String, NewFilename As %String) as %Boolean [ Language = objectscript ]
Rename a file from OldFilename to NewFilename.
method Retrieve(Filename As %String, ByRef Stream As %AbstractStream) as %Integer [ Language = objectscript ]
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.
method RetryRetrieve(Filename As %String, Stream As %AbstractStream) as %Integer [ Language = objectscript ]
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.
Set the directory on the Ftp server to Path.
method SetToParentDirectory() as %Boolean [ Language = objectscript ]
Move to parent directory on the Ftp server.
If the FTP server supports it then this will return a files size based on the current transfer type in the Size output argument. Returns true if it completed correctly.
Return the status of the Ftp server in the Status parameter which should be passed by reference.
method Store(Filename As %String, Stream As %AbstractStream) as %Boolean [ Language = objectscript ]
Store the data in Stream in the Filename on the Ftp server.
method StoreFiles(Directory As %String, Wildcards As %String, Delimiter As %String = ";") as %Boolean [ Language = objectscript ]
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
Return information about the type of computer you are connected to in the by reference parameter System.
method TypeGet() as %String [ Language = objectscript ]
Return true if this IPv4 address is a private address
method sendCommand(Command As %String, noread As %Boolean = 0) as %Boolean [ Language = objectscript ]