I'm trying to download a file from the internet using code. It's a graphic file. I have learned that Inet is the best way to do this, but it's still not working for me. Does anyone have any code to do this? Thanks.
Printable View
I'm trying to download a file from the internet using code. It's a graphic file. I have learned that Inet is the best way to do this, but it's still not working for me. Does anyone have any code to do this? Thanks.
IE downloads all files this way:
Code:Private Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: [email protected]
DoFileDownload StrConv("http://www.microsoft.com/", vbUnicode)
End Sub
OK, that's great. I'm trying to get it to download specific files off a website and save them to the harddrive on my local machine?
This is the code I already have, but it doesn't seem to work for image files. It will download the file, but it's empty. Do you know why that would be?
Private Sub Command1_Click()
Text1.Text = Inet1.openURL("http://www.microsoft.com/vbasic/default.htm")
End Sub
Private Sub Command2_click()
Dim bytdata() As Integer
bytdata() = Inet1.openURL("Http://www.microsoft.com/library/images/gifs/toolbar/home.gif", icByteArray)
bytdata() = Inet1.openURL
Open "C:\vbbb\http\home.gif" For Binary Access Write As #1
Put #1, , bytdata()
Close #1
MsgBox "home.gif downloaded"
End Sub
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 Public 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 Form_Load() 'example by Matthew Gates ([email protected]) DownloadFile "http://www.allapi.net", "c:\allapi.htm" End Sub
In your case I guess you would use it like this:
VB Code:
DownloadFile "Http://www.microsoft.com/library/images/gifs/toolbar/home.gif", "C:\vbbb\http\home.gif"
That still won't work. The size of the file is wrong. And when I go to bring it up it's 5kb instead of 56kb??????
I've just tried the following code and it downloaded the gif without problem:
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 Public 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 Form_Load() DownloadFile "Http://www.microsoft.com/library/images/gifs/toolbar/home.gif", "c:\temp\home.gif" End Sub
When I used IE to look at the file, it is only 211 bytes - not 5 or 56kb.
maybe it is still ownloading. if you have a 14 k modem it might take a while......