9

Does anyone know how to download a file (without opening a webpage), and save it to a directory in Visual Basic 6.0?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
lab12
  • 6,400
  • 21
  • 68
  • 106

7 Answers7

7

If you want to do it with code only (no Internet Transfer Control), VBNet.mvps.org has a really good how-to article that uses the URLDownloadToFile API call.

From the article:

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.

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 BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000

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

FYI - in testing on Windows 7, it would only return the cached version, so I had to use the extra function mentioned in the article to clear it first (and that worked).

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
   Alias "DeleteUrlCacheEntryA" _
  (ByVal lpszUrlName As String) As Long

Then just call the above function with the destination URL first, to clear the cache.

C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67
  • 3
    You **don't need API calls**, you **don't need the Internet Transfer control**. Just do it the easy way, using native VB6 code. http://visualstudiomagazine.com/articles/2008/03/27/simple-asynchronous-downloads.aspx – MarkJ Feb 24 '10 at 15:42
  • +1, thank you very much for introducing me to these API calls. – Patrick Moore Jul 07 '12 at 05:55
6

You don't need API calls, you don't need the Internet Transfer control. Just do it the easy way, using native VB6 code. Here's an excellent article by Karl Peterson with sample code.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • There was something I had never seen before. I have a full class using the WININET library so if I need to do this in VB again I will probably stick with that, but it is always good to have alternatives. – jac Dec 30 '09 at 14:17
  • If Internet explorer's offline property is true this won't work. You'll get a file not found error. – kjack May 07 '13 at 06:59
4

Try this

Sub DownloadFile(url, path)

   Dim objReq
   Dim objStream

   Set objReq = CreateObject("MSXML2.XMLHTTP")
   objReq.Open "GET", url, False
   objReq.send

   If objReq.Status = 200 Then
       Set objStream = CreateObject("ADODB.Stream")
       objStream.Open
       objStream.Type = 1

       objStream.Write objReq.ResponseBody
       objStream.Position = 0

       objStream.SaveToFile path, 2
       objStream.Close
       Set objStream = Nothing
   End If

   Set objReq = Nothing

End Sub
Palanikumar
  • 6,940
  • 4
  • 40
  • 51
0

I would suggest using the Internet Transfer Control

cmsjr
  • 56,771
  • 11
  • 70
  • 62
0

You need to use the Internet Transfer control, see http://www.vb-helper.com/howto_get_file_from_web.html for a sample. If you need to specify credentials, check out http://support.microsoft.com/kb/173264 as well.

stuartd
  • 70,509
  • 14
  • 132
  • 163
0

I don't like the Internet Transfer Control because it is synchronous. Once you start a download your application is unresponsive until the file is downloaded or an error is thrown. There are plenty of good examples of using the WININET DLL to write asynchronous methods. It is not trivial, but it is also very do-able. Here is an example from stackoverflow.

Community
  • 1
  • 1
jac
  • 9,666
  • 2
  • 34
  • 63
  • It **is** trivial to do asynchronous downloads in VB6 and it doesn't require API calls. See my answer http://stackoverflow.com/questions/1976152/download-file-vb6/1979417#1979417 – MarkJ Dec 30 '09 at 10:17
-2

Try This:

My.Computer.Network.DownloadFile (*File to download*, *What to save it as*)

You must give it a filename in the what to save it as:

Example:

 My.Computer.Network.DownloadFile _
("http://www.cohowinery.com/downloads/WineList.txt", _
"C:\Documents and Settings\All Users\Documents\WineList.txt")
Cal
  • 13
  • 1