The URL property returns the current Uniform Resource
Locator string which is used by the control to access a file on the
server. URLs have a specific format which provides information about
the server, port, path and file name, as well as optional
information such as a username and password for authentication:
[ftp|ftps|sftp]://[username:[password]@]hostname[:port]/[path/...]filename[;type=id]
The first part of the URL is the scheme, and in this case will
always be "ftp", "ftps" or "sftp"
depending on which protocol is required. If a username and password
is required for authentication, then this will be included in the URL
before the name of the server; otherwise an anonymous FTP
session is assumed. Next, there is the name of the server to
connect to, optionally followed by a port number. If no port number
is given, then the default port for the protocol will be used. This
is followed by the path, and then the name of the file on the server.
An optional file type may be specified as well, with the type
identifier being either "a" for text files or "i"
for binary files.
One important consideration when using FTP URLs is that the path
is relative to the user's home directory and should not be considered
an absolute path from the root directory on the server. If no
username and password is provided, then an anonymous session is used
and the path is relative to the public directory used by the FTP
server.
Here are some common examples of URLs used to access files on an
FTP server:
ftp://www.example.com/pub/financial/jan2023.xlsx
In this example, the server is www.example.com, the path is
"pub/financial" and the file name is
"jan2023.xlsx". The default port will be used to access
the file, and no username and password is provided for
authentication so this file must be publicly available to
anonymous users.
ftp://www.example.com:2121/employees/picnic.docx
In this example, the server is www.example.com, the path is
"employees" and the file name is "picnic.docx".
However, the client should connect to an alternative port number,
in this case 2121. This file must also be available to anonymous
users because no username or password has been specified.
ftps://executive:secret@www.example.com/corporate/projections/sales2024.xlsx
In this example, the server is www.example.com and, the path
is "corporate/projections" and the file name is
"sales2024.xlsx". Because the protocol is ftps, a secure
connection on port 990 will be established. The user name
"executive" and password "secret" will be used
to authenticate the session.
When setting the URL property, the control will parse the
string and automatically update the HostName,
RemotePort, UserName, Password,
RemotePath and RemoteFile properties according to the
values specified in the URL. This enables an application to simply
provide the URL and then call the Connect method to establish
the connection.
Note that if this property is assigned a value which cannot be
parsed, the control will throw an error that indicates that the
property value is invalid. In a language like Visual Basic it is
important that you implement an error handler, particularly if you
are assigning a value to the property based on user input. If the
user enters an invalid URL and there is no error handler, it could
result in an exception which terminates the application.
' Setup error handling since the control will throw an error
' if an invalid URL is specified
On Error Resume Next: Err.Clear
FtpClient1.URL = Text1.Text
' Check the Err object to see if an error has occurred, and
' if so, let the user know that the URL is invalid
If Err.Number <> 0 Then
MsgBox "The specified URL is invalid", vbExclamation
Text1.SetFocus
Exit Sub
End If
' Reset error handling and connect to the server using the
' default property values that were updated when the URL
' property was set (ie: HostName, RemotePort, UserName, etc.)
On Error GoTo 0
nError = FtpClient1.Connect()
If nError > 0 Then
MsgBox FtpClient1.LastErrorString, vbExclamation
Exit Sub
End If
' Change to the directory specified by the RemotePath property
nError = FtpClient1.ChangeDirectory(FtpClient1.RemotePath)
If nError > 0 Then
MsgBox FtpClient1.LastErrorString, vbExclamation
Exit Sub
End If
' Download the file to the local system
strLocalFile = strLocalPath & "\" & FtpClient1.RemoteFile
nError = FtpClient1.GetFile(FtpClient1.RemoteFile, strLocalFile)