Downloading Files

The GetFile method may be used after connecting to an FTP or HTTP server with the Connect method. The GetMultipleFiles method may only be used with an FTP server.

In the following Visual Basic example, an instance of the File Transfer control has been placed on a form. The form also contains edit controls for server name, port, user name, password, remote file, and local file. In addition, there is an array of three option controls used to select the server type. The server type is initialized in the Form_Load event. The indices 0,1,2 correspond to the possible values for the ServerType property. There is also a check box used to select whether SSL Authorization is to be used for a secure FTP server. The Options property is initialized in the Form_Load event.

If the Server Type is left undefined, then the control will try to infer the type from the port number.

Private Sub Form_Load()
    optServer_Click fileServerUndefined
    chkAuthSSL_Click
End Sub

Private Sub optServer_Click(Index As Integer) 
    FileTransfer1.ServerType = Index
End Sub

Private Sub chkAuthSSL_Click()
    If chkAuthSSL.Value = 0 Then
        FileTransfer1.Options = FileTransfer1.Options And (Not fileOptionSecureExplicit)
    Else
        FileTransfer1.Options = FileTransfer1.Options Or fileOptionSecureExplicit
    End If
End Sub

The code for the "Download" button reads the edit controls, connects to a server (FTP or HTTP), downloads a file, and disconnects:

Private Sub cmdDownload_Click()
    Dim lResult as Long
    
    On Error GoTo Err_report
    FileTransfer1.UserName = editUserName.Text
    FileTransfer1.Password = editPassword.Text
    FileTransfer1.ServerPort = editPort.Text
    
    lResult = FileTransfer1.Connect(editServer.Text)
    If lResult <> 0 Then
        MsgBox "Connect Failed" & vbCrLf & FileTransfer1.LastErrorString
        Exit Sub
    End If
    
    lResult = FileTransfer1.GetFile(editLocalFile.Text, editRemoteFile.Text)
    If lResult <> 0 Then
        MsgBox "Download Failed" & vbCrLf & FileTransfer1.LastErrorString
    Else
        MsgBox FileTransfer1.TransferBytes & " bytes transferred"
    End If
    FileTransfer1.Disconnect
    Exit Sub
Err_report:
    MsgBox Err.Number & ": " & Err.Description
    FileTransfer1.Disconnect
    
End Sub

Copyright © 2008 Catalyst Development Corporation. All rights reserved.