What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleUse the Internet Transfer Control to upload a file to the Web
DescriptionThis example shows how to use the Internet Transfer Control to upload a file to the Web in Visual Basic 6.
KeywordsInternet Transfer Control, inet, upload, ftp
CategoriesInternet, Utilities
When you click the Get File button, the program calls subroutine UploadFile, passing it source and destination file names, the host name, user name, and password.
' Upload the file.
Private Sub cmdUploadFile_Click()
    Screen.MousePointer = vbHourglass

    If UploadFile(txtFromFile.Text, txtToFile.Text, _
        txtHostName.Text, txtUserName.Text, _
        txtPassword.Text) Then
        MsgBox "Upload Complete", _
            vbOKOnly Or vbInformation, _
    End If

    Screen.MousePointer = vbDefault
End Sub
Subroutine UploadFile uses an Internet Transfer Control. It sets the control's URL, UserName, and Passsword properties. It then uses the Execute method to run the FTP PUT command. If you have the wrong host, user name, or password, the program should catch an error. If the syntax is incorrect or if you try to upload into the wrong directory, the method may fail silently.
' Upload a file. Return True if we are successful.
Private Function UploadFile(ByVal source_file As String, _
    ByVal dest_file As String, ByVal host_name As String, _
    ByVal user_name As String, ByVal passwd As String) As _
    ' Get the file's contents.
    On Error GoTo UploadError

    ' You must set the URL before the user name and
    ' password. Otherwise the control cannot verify
    ' the user name and password and you get the error:
    '       Unable to connect to remote host
    If LCase$(Left$(host_name, 6)) <> "ftp://" Then _
        host_name = "ftp://" & host_name
    inetFtp.URL = host_name
    inetFtp.UserName = user_name
    inetFtp.Password = passwd

    ' Do not include the host name here. That will make
    ' the control try to use its default user name and
    ' password and you'll get the error again.
    inetFtp.Execute , "Put " & source_file & " " & dest_file

    UploadFile = True
    Exit Function

    MsgBox "Error " & Err.Number & _
        " uploading file '" & _
        source_file & "' to '" & _
        dest_file & "'." & vbCrLf & Err.Description, _
        vbExclamation Or vbOKOnly, _
        "Download Error"
    UploadFile = False
    Exit Function
End Function
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.