download file from net - w/o events
am looking for a really simple (and compatable) way of downloading a file from the net - i also want my app not to run the next line of code until this has happened
i have seen api to do this before but cant seem to find any atm
thanks in advance
Kris Bennett
Re: download file from net - w/o events
Try this. You need a form with an Internet Transfer Control called inetDownload on it.
VB Code:
Option Explicit
Private Sub Form_Load()
DownLoadFromURL "http://www.yourwebsite.com/files/", "filetodownload.txt"
Unload Me
End Sub
Private Sub DownLoadFromURL(ByVal url As String, ByVal filename As String)
'
' Download a file from a given URL
'
Dim bytes() As Byte
Dim fnum As Integer
Dim ftext As String
'
' Download the file and load into byte array
'
ftext = url & filename
bytes() = inetDownload.OpenURL(ftext, icByteArray)
fnum = FreeFile
'
' Write the downloaded file to disk
'
Open App.Path & "\" & filename For Binary Access Write As #fnum
Put #fnum, , bytes()
Close #fnum
End Sub
Re: download file from net - w/o events
um does any one no how to do it without using 3rd party controls?
thanks
Kris
Re: download file from net - w/o events
It took about 5 seconds of searching to turn up this link....
http://www.devx.com/vb2themax/Tip/19203
Remember... Google is your friend
Re: download file from net - w/o events
Or you can use the URLDownloadToFile API function.
VB Code:
Private Declare Function URLDownloadToFile Lib "URLMON.dll" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, _
ByVal szUrl As String, _
ByVal szFileName As String, _
ByVal dwFlags As Long, _
ByVal lpfnCB As Long _
) As Long
Public Sub DownloadFile(ByVal sUrl As String, ByVal sLocalFile As String)
Call URLDownloadToFile(0&, sUrl, sLocalFile, 0&, 0&)
End Sub
Simply use it like this:
VB Code:
Call DownloadFile("http://www.domain.com/theFile.zip", "c:\SomePath\theFile.zip")
Re: download file from net - w/o events
Quote:
Originally Posted by Joacim Andersson
Or you can use the URLDownloadToFile API function
You just need to be aware that this requires that Internet Explorer 5 or greater is installed.
Re: download file from net - w/o events
Quote:
Originally Posted by pnish
You just need to be aware that this requires that Internet Explorer 5 or greater is installed.
Is there anyone who still use IE4 or earlier :)
Re: download file from net - w/o events
The sad truth is that you never really know until your app breaks... ;)
Re: download file from net - w/o events
VB Code:
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 Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function
Private Sub Command1_Click()
If DownloadFile("http://www.mywebsite.com/somefile.exe", "c:\somefile.exe") Then
Debug.Print "Download Complete"
End If
End Sub
Re: download file from net - w/o events
Quote:
Originally Posted by pnish
The sad truth is that you never really know until your app breaks... ;)
Well, putting IE5 or later as a requirement for your application should not be a problem. Besides you can always use code like the following to test if it's installed or not before calling URLDownloadToFile
VB Code:
Private Declare Function DllGetVersion Lib "shlwapi" ( _
dwVersion As DllVersionInfo _
) As Long
Private Type DllVersionInfo
cbSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformID As Long
End Type
Public Function UsesIE5(ByRef sVersionString As String) As Boolean
Dim dvi As DllVersionInfo
dvi.cbSize = Len(dvi)
Call DllGetVersion(dvi)
sVersionString = dvi.dwMajorVersion & "." & dvi.dwMinorVersion & "." & _
dvi.dwBuildNumber
UsesIE5 = (dvi.dwMajorVersion >= 5)
End Function
Now simply use this:
VB Code:
Dim sIEVer As String
If UsesIE5(sIEVer) = False Then
MsgBox "You are currently running version " & sIEVer & " of Internet Explorer" & vbCrLf & _
"This application requires version 5.0 or later.", vbInformation
End If
Re: download file from net - w/o events
Quote:
Originally Posted by Joacim Andersson
Well, putting IE5 or later as a requirement for your application should not be a problem. Besides you can always use code like the following to test if it's installed or not before calling URLDownloadToFile
I agree. The whole point of my post was to make i00 aware of the restriction.
Re: download file from net - w/o events
@Rory. What's the difference between your post and the code I posted earlier?
Re: download file from net - w/o events
Its a function so it tells the user when it is downloaded . ..
Re: download file from net - w/o events
Quote:
Originally Posted by rory
Its a function so it tells the user when it is downloaded . ..
No it doesn't. It tells the user if it was able to download it perhaps. Neither the function nor the sub returns until the file is downloaded (or the download failed for some reason).
Re: download file from net - w/o events
Quote:
Originally Posted by Joacim Andersson
No it doesn't. It tells the user if it was able to download it perhaps. Neither the function nor the sub returns until the file is downloaded (or the download failed for some reason).
thats what i said .. it tells them when it is downloaded. (not when it is downloading)
Edit. .. whoops excuse my Bahamian, that should be "once it has completed the download" .. (1am to my defence) :bigyello:
Re: download file from net - w/o events
this one gives you a progress as it is downloaded .. check it out ..
just threw it together tonight :D
ps. not for large file downloads. ..