Visual Basic Internet Routines
URLDownloadToFile: Fast, Simple and Transparent File Downloads Bypassing the IE Cache
     
Posted:   Friday June 18, 2004
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

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

     
 Prerequisites
IE3 or later installed.

This demo duplicates the basic URLDownloadToFile routine at URLDownloadToFile: Fast, Simple and Transparent File Downloads and adds code to call DeleteUrlCacheEntry prior to the download.  This will nuke any cached copy of the file to ensure the remote copy is always obtained. Please see URLDownloadToFile: Fast, Simple and Transparent File Downloads for a description of the URLDownloadToFile API.

For the record, the illustration here simply shows a HTML file downloaded with this code 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 -- the demo could have just as easily shown a downloaded image in a picture box, or a mpg file in the animation control. The code is not limited to downloading only plain-text files.

 BAS Module Code
None.

 Form Code
If you created the demo from URLDownloadToFile: Fast, Simple and Transparent File Downloads, just add Command2 and the RED/BOLD code below. If you are creating this demo from scratch add two command buttons (Command1/Command2), two labels (Label1 and Label2), and a text box (Text1) onto a form and set the text box Multiline property to True. The illustration above is reused from the original demo page and thus only shows one command button.

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 Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
   Alias "DeleteUrlCacheEntryA" _
  (ByVal lpszUrlName As String) As Long
   
Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000


Private Sub Form_Load()

   Command1.Caption = "Download File"
   Command2.Caption = "Clear Cache && Download"
   
End Sub

   
Private Sub Command1_Click()

   Dim sSourceUrl As String
   Dim sLocalFile As String
   Dim hfile As Long
   
   sSourceUrl = "http://vbnet.mvps.org/code/faq/fileloadtext.htm"
   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


Private Sub Command2_Click()

   Dim sSourceUrl As String
   Dim sLocalFile As String
   Dim hfile As Long
   
   sSourceUrl = "http://vbnet.mvps.org/code/faq/fileloadtext.htm"
   sLocalFile = "c:\deleteme.htm"
   
   Label1.Caption = sSourceUrl
   Label2.Caption = sLocalFile
   
  'Attempt to delete any cached version of
  'the file. Since we're only interested in
  'nuking the file, the routine is called as
  'a sub. If the return value is requires
  '(calling as a function), DeleteUrlCacheEntry
  'returns 1 if successful, or 0 otherwise, e.g.
  '  If DeleteUrlCacheEntry(sourceUrl) = 1 Then
  '     Debug.Print "cached file found and deleted"
  '  Else
  '     Debug.Print "no cached file for " & sourceUrl
  '  End If
  'Note that the remote URL is passed as this is the
  'name the cached file is known by. This does NOT 
  'delete the file from the remote server.
   Call DeleteUrlCacheEntry(sSourceUrl)
   
   If DownloadFile(sSourceUrl, sLocalFile) = True Then

         hfile = FreeFile
         Open sLocalFile For Input As #hfile
            Text1.Text = Input$(LOF(hfile), hfile)
         Close #hfile

   End If
   
End Sub


Private 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
 Comments
The full extent of this additional code will be seen if you have access to the test file and can make changes between tests. Otherwise try clearing your IE cache, download the file using Command1 (a refresh of the Explorer cache folder may be necessary to see the new file), then place a breakpoint on the DownloadFile line and hit Command2. The cached copy of the file downloaded with Command1 should disappear (again a refresh may be required). Continuing the code will re-download the file.

 
 

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