Uploading Files  
 

The PutFile method is used to upload files using either the FTP or HTTP controls. In its simplest form, the method accepts two arguments, the name of the local file to upload and the name of the file that will be created or overwritten on the server. For example, consider a form which has five TextBox controls and a CommandButton control:

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

    FtpClient1.HostName = Trim(Text1.Text)
    FtpClient1.UserName = Trim(Text2.Text)
    FtpClient1.Password = Trim(Text3.Text)
    strLocalFile = Trim(Text4.Text)
    strRemoteFile = Trim(Text5.Text)

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

    ' Download the file to the local system
    nError = FtpClient1.PutFile(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, the user specifies the name of a server, a username and a password. In most cases, servers only permit authenticated users to upload files, so this information is usually required. This same code would work with the HTTP control to upload a file to 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.