Download File - VB6

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

+6
vb6 download
source share
7 answers

If you want to do this only with code (without Internet port control), VBNet.mvps.org has a really good practical article that uses the URL URL URLDownloadToFile API.

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 the name and path and name of the local file, the API loads a bit of the specified file and saves them as the target name. The function works with all types of files - plain text, images, html, mpg, wav and zip files, etc. without changing the procedure or caring for the download file, and there is no visible size limitation 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 - when testing in Windows 7, it will only return the cached version, so I had to use the additional function mentioned in the article to clear it first (and it worked).

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

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

+6
source share

You do not need API calls; you do not need an Internet Transfer control. Just do it easily using your own VB6 code. Here's a great article by Karl Peterson with a sample code.

+6
source share

try it

 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 

see http://smartreferences.blogspot.in for details

+1
source share

I would suggest using Internet Access Control

0
source share

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 provide credentials, check out http://support.microsoft.com/kb/173264 .

0
source share

I donโ€™t like Internet transmission control because it is synchronous. After you start the download, your application does not respond until the file is downloaded or an error is thrown. There are many good examples of using the WININET DLL to write asynchronous methods. This is not trivial, but it is also very useful. Here is an example from stackoverflow.

0
source share

Try the following:

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

You must tell him the name of the file in order 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") 
-one
source share

All Articles