Visual Basic Internet Routines
URLDownloadToFile: Fast, Simple and Transparent File Downloads
Posted:   Friday February 22, 2002
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   VBnet - Randy Birch


URLDownloadToFile: Fast, Simple and Transparent File Downloads
URLDownloadToFile: Fast, Simple and Transparent File Downloads Bypassing the IE Cache

DoFileDownload: Download Files Using IE's Download Dialog
DoFileDownload: Customize the IE Download Dialog

FtpFindFirstFile: Connect and Retrieve FTP File Listings

FtpFindFirstFile: Download Files via FTP with a Download Progress Callback

IE3 or later installed.

The URLDownloadToFile API is available on all versions of the Windows operating system (except Win3, WinNT3.x). By passing the remote file name and the local file path and name, the API downloads the bits of the specified file saving them as the target name. The function works with all file types - plain text, images, html, mpg, wav and zip files etc. without modification to the routine or concern for the file being downloaded, nor is there any apparent size restriction or limitation.

The downloaded file can then be either opened and displayed in VB (as the illustration shows for a html file), or the path and filename can be passed to ShellExecute or ShellExecuteEx for opening in the associated application.  For multiple files It downloads them one at a time, and the wrapper returns true if the download of each file was successful.

The pCaller member of the API is used to specify the address of the controlling IUnknown interface when the call is used in an ActiveX controls. For use in an application, 0& (null) is passed.

UrlDownloadToFile hauls the file down to the IE cache, then copies the file to the local or remote file system destination you specify. Because it uses the cache, subsequent calls to this API will pull the cached copy, not the copy off the site. Where assurance is needed that the call s always pulling from the site, you need to delete the cached copy of the file first. Thankfully Windows provides a one-liner for that too, and it - along with UrlDownloadToFile - is demonstrated in the code at URLDownloadToFile: Fast, Simple and Transparent File Downloads Bypassing the IE Cache.

Once again, the illustration above simply shows a HTML file downloaded with this code and displayed in a text box as proof of success. This API can download anything, so is not limited to simply downloading HTML plain-text files.

Note: While the link above provides a mechanism for ensuring the file is hauled from the remote site, there has been discussion about possibly getting this particular demo to also bypass the cache.  The next two paragraphs speak to that particular issue in the context of this demo.  If you elect to use the code from the link above, the following discussion can be ignored.

The fourth parameter in URLDownloadToFile is named as a reserved variable (dwReserved) and as such is normally not used. However, KB article 196466 indicates that a user can specify BINDF_GETNEWESTVERSION to force the API to download from the web server rather than retrieve a local cached copy of the file, if available. The code below shows BINDF_GETNEWESTVERSION in use.

In addition, some newsgroup posts have indicated passing 2 as dwReserved does the same thing, and the KB article mentioned also states BINDF_GETNEWESTVERSION maps to INTERNET_FLAG_RELOAD when the target is a URL. This therefore means there may be three possible values that are valid ... decimal 16 / hex &10 (BINDF_GETNEWESTVERSION ), decimal 2 / hex &H2 (undefined, possibly BINDF_ASYNCSTORAGE) and decimal -2147483648 / hex &H80000000 (INTERNET_FLAG_RELOAD). You'll need to experiment to confirm if any of these in fact do pull the remote file over the cached version.

This code in this demo loosely based on a msnews newsgroup posting by Matthew Gates.

 BAS Module Code

 Form Code
Drop a command button (Command1), two labels (Label1 and Label2), and a text box (Text1) onto a form and set the Multiline property to True. Add the following code and change the remote URL to a valid file on a server, and the local file name to valid a valid folder on your system:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Declare Function URLDownloadToFile Lib "urlmon" _
   Alias "URLDownloadToFileA" _
  (ByVal pCaller As Long, _
   ByVal szURL As String, _
   ByVal szFileName As String, _
   ByVal dwReserved As Long, _
   ByVal lpfnCB As Long) As Long
Private Const ERROR_SUCCESS As Long = 0
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000

Private Sub Form_Load()

   Command1.Caption = "Download File"
End Sub

Private Sub Command1_Click()

   Dim sSourceUrl As String
   Dim sLocalFile As String
   Dim hfile As Long
   sSourceUrl = ""
   sLocalFile = "c:\deleteme.htm"
   Label1.Caption = sSourceUrl
   Label2.Caption = sLocalFile
   If DownloadFile(sSourceUrl, sLocalFile) Then
      hfile = FreeFile
      Open sLocalFile For Input As #hfile
         Text1.Text = Input$(LOF(hfile), hfile)
      Close #hfile
   End If

End Sub

Public Function DownloadFile(sSourceUrl As String, _
                             sLocalFile As String) As Boolean
  'Download the file. BINDF_GETNEWESTVERSION forces 
  'the API to download from the specified source. 
  'Passing 0& as dwReserved causes the locally-cached 
  'copy to be downloaded, if available. If the API 
  'returns ERROR_SUCCESS (0), DownloadFile returns True.
   DownloadFile = URLDownloadToFile(0&, _
                                    sSourceUrl, _
                                    sLocalFile, _
                                    BINDF_GETNEWESTVERSION, _
                                    0&) = ERROR_SUCCESS
End Function


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter