Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the Internet Transfer Control to download a file from the Web
DescriptionThis example shows how to use the Internet Transfer Control to download a file from the Web in Visual Basic 6.
KeywordsInternet Transfer Control, inet, download, ftp
CategoriesInternet, Utilities
 
When you click the Get File button, the program calls subroutine DownloadFile, passing it source and destination file names.
 
' Get the file.
Private Sub cmdGetFile_Click()
    Screen.MousePointer = vbHourglass
    DoEvents

    If DownloadFile(txtFromUrl.Text, txtToFile.Text) Then
        MsgBox "Download Complete", _
            vbOKOnly Or vbInformation, _
            "Done"
    End If

    Screen.MousePointer = vbDefault
End Sub
 
Subroutine DownloadFile uses an Internet Transfer Control. It calls the control's OpenURL method, saving the result in a byte array. It then writes the array into a file and returns. Note that if the file is missing, the server will return a "File not found" page, which this subroutine will save into the destination file. The calling program should veify that the file's contents look correct.
 
' Download a file. Return True if we are successful.
Private Function DownloadFile(ByVal source_file As String, _
    ByVal dest_file As String) As Boolean
Dim bytes() As Byte
Dim fnum As Integer

    ' Get the file's contents.
    On Error GoTo DownloadError
    bytes() = inetFtp.OpenURL(source_file, icByteArray)

    ' Remove the file if it exists.
    On Error Resume Next
    Kill dest_file
    On Error GoTo DownloadError

    ' Write the contents into the destination file.
    fnum = FreeFile
    Open dest_file For Binary Access Write As #fnum
    Put #fnum, , bytes()
    Close #fnum

    DownloadFile = True
    Exit Function

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