Downloading Files  
 

The GetFile method is used to download files using either the FTP or HTTP controls. In its simplest form, the method accepts two arguments, the name of the local file to create or overwrite and the name of the file on the server to download. For example, consider a form with three TextBox controls and a CommandButton control:

Private Sub Command1_Click()
    Dim strHostName As String
    Dim strLocalFile As String
    Dim strRemoteFile As String
    Dim nError As Long

    strHostName = Trim(Text1.Text)
    strLocalFile = Trim(Text2.Text)
    strRemoteFile = Trim(Text3.Text)

    ' Establish a connection to the server and display any
    ' errors to the user
    nError = FtpClient1.Connect(strHostName)
    If nError Then
        MsgBox FtpClient1.LastErrorString, vbExclamation
        Exit Sub
    End If

    ' Download the file to the local system
    nError = FtpClient1.GetFile(strLocalFile, strRemoteFile)
    If nError Then
        MsgBox FtpClient1.LastErrorString, vbExclamation
        FtpClient1.Disconnect
        Exit Sub
    End If

    ' Disconnect from the server
    FtpClient1.Disconnect
End Sub

In this example, there is no user name or password specified so the FTP server would need to accept anonymous logins. This same code would work with the HTTP control to download a file from a web server. To provide some feedback to the user as to the status of the transfer, a ProgressBar control could be added to the form. To update the progress bar, a handler for the OnProgress event would also be added:

Private Sub FtpClient1_OnProgress(ByVal FileName As Variant, _
                                  ByVal FileSize As Variant, _
                                  ByVal BytesCopied As Variant, _
                                  ByVal Percent As Variant)
    ProgressBar1.Value = Percent
End Sub

Simply setting the Value property of the ProgressBar control to the Percent argument in the event handler will update the progress bar during the file transfer process. As with the previous code, this can also be done using the HTTP control in the same way.

For a second example, let's use the HTTP control to download an HTML page from a website and store it on the local system. The form has two TextBox controls and a CommandButton control. The first TextBox control will contain the URL of the page to download, and the second control will specify the local name on the server.

Private Sub Command1_Click()
    Dim strHostName As String
    Dim strLocalFile As String
    Dim strRemoteFile As String
    Dim nError As Long

    ' If the user enters an invalid URL, setting the URL property will
    ' throw an exception, so that needs to be handled here
    On Error Resume Next: Err.Clear
    HttpClient1.URL = Trim(Text1.Text)
    
    If Err.Number Then
        MsgBox "An invalid URL has been specified", vbExclamation
        Exit Sub
    End If
    
    On Error GoTo 0
    strLocalFile = Trim(Text2.Text)

    ' Establish a connection to the server and display any
    ' errors to the user
    nError = HttpClient1.Connect()
    If nError Then
        MsgBox HttpClient1.LastErrorString, vbExclamation
        Exit Sub
    End If

    ' Download the file to the local system
    nError = HttpClient1.GetFile(strLocalFile, HttpClient1.Resource)
    If nError Then
        MsgBox HttpClient1.LastErrorString, vbExclamation
        HttpClient1.Disconnect
        Exit Sub
    End If

    ' Disconnect from the server
    HttpClient1.Disconnect
End Sub

You'll notice that much of the code is very similar, with the exception for the error handling code used when the URL property is set, and the use of the Resource property. When the URL property is set, the control will automatically parse the URL and update the related properties such as HostName and RemotePort. The path and file name portion of the URL is assigned to the Resource property, which can then be used in conjunction with the methods such as GetFile and PutFile.