|
-
Nov 24th, 2005, 09:37 PM
#1
Thread Starter
Addicted Member
Download file from Internet problems [RESOLVED]
Hi all,
I'm trying to make a program that downloads first a text file from the internet, and then some exes, and runs them (an updater program, if you like).
I've researched on this forum a bit and downloaded a few sample programs / code snippits for this kind of thing, but I'm habing some troubles.
Problem 1:
A common code snippit seems to be the "Inet1.openURL" code. Here's the section in my test program:
Code:
Private Sub Command2_Click()
Dim TextIn As String
TextIn = Inet1.OpenURL("http://arbital24.com/Test.txt", icString)
MsgBox TextIn
End Sub
And another, using a similar code but this time designed to actually download a file, rather than just reading the text:
Code:
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() = Inet1.OpenURL(ftext, icByteArray)
fnum = FreeFile
'
' Write the downloaded file to disk
'
Open "C:\" & filename For Binary Access Write As #fnum
Put #fnum, , bytes()
Close #fnum
End Sub
Private Sub Command3_Click()
DownLoadFromURL "http://arbital24.com/123.exe", "123.exe"
Unload Me
End Sub
With either of the code snippits, if I try and run the code and click the button, whenever it gets to the "Inet1.OpenURL" line, it comes back with an error saying "Run-time error 424 - Object required"
Why is this? And how can I stop it? I registered the "Microsoft Internet Transfer Control 6.0" by clicking Project > Components
Problem 2:
I've tried this code snippit:
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 Command1_Click()
Dim TextIn As String
Dim FF As Integer
FF = FreeFile()
DownloadFile "http://arbital24.com/123.exe", "c:\123.exe"
End Sub
But what actually downloads is a HTML file of a page. I tested this first using the "Test.txt" file, which came back with the same result. The contents of the HTML file:
Code:
<HTML><HEAD>
<META NAME="description" content="">
<META NAME="keywords" content="">
<TITLE></TITLE>
</HEAD>
<FRAMESET ROWS="100%,*" BORDER="0" FRAMEBORDER="0">
<FRAME SRC="http://arbital24.dyndns.org/123.exe" SCROLLING="AUTO" NAME="bannerframe" NORESIZE>
</FRAMESET>
<NOFRAMES>
<P>
<DIV ALIGN="CENTER"><A HREF="http://arbital24.dyndns.org/123.exe">http://arbital24.com/</A></DIV>
</NOFRAMES>
</HTML>
And the same code for the Test.txt file. In both cases, opening the file in Internet explorer will download/load the .exe or .txt file
So for this code snippit, How can I actually get it to download the contents of the file, rather than just some random HTML file?
Thanks for any help,
Arby
EDIT:
I've found that if I add to the form a blank "Inet1" object, I no longer get the "Object required" error (Problem 1). However, the file still loads (in all 3 code snippits) that blank HTML file. So, I just need to know how to make it load the actual file, and not this HTM
Last edited by BubbleLife; Nov 24th, 2005 at 10:43 PM.
-
Nov 24th, 2005, 09:55 PM
#2
Re: Download file from Internet problems
Here is a program I wrote for my dad to download a file every week.
VB Code:
Option Explicit
Private Sub Form_Load()
DownLoadFromURL "http://.../~phga/", "Hdcpreco.phg"
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() = Inet1.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
GetFileForDad
-
Nov 24th, 2005, 10:02 PM
#3
Thread Starter
Addicted Member
Re: Download file from Internet problems
Ahh, thank you, sadly I've actually used that code in one of my sample code snippits (see my second text box). Here is another version that I ust tested:
Code:
Option Explicit
Private Sub Form_Load()
DownLoadFromURL "http://arbital24.com/123.exe", "13.exe"
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() = Inet1.OpenURL(ftext, icByteArray)
fnum = FreeFile
'
' Write the downloaded file to disk
'
Open "c:\" & filename For Binary Access Write As #fnum
Put #fnum, , bytes()
Close #fnum
End Sub
Sadly, I'm still having the problem where it just downloads an empty file with that section of HTML code in it as described in the first post
-
Nov 24th, 2005, 10:06 PM
#4
Re: Download file from Internet problems
I don't have the filename in the url. I think you are passing the filename twice, which is why you are getting bad data.
-
Nov 24th, 2005, 10:11 PM
#5
Thread Starter
Addicted Member
Re: Download file from Internet problems
Filename where it says "DownLoadFromURL "http://arbital24.com/123.exe", "13.exe""?
Well, Just tested it again using the following code:
Code:
Option Explicit
Private Sub Form_Load()
DownLoadFromURL "http://arbital24.com/", "123.exe"
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() = Inet1.OpenURL(ftext, icByteArray)
fnum = FreeFile
'
' Write the downloaded file to disk
'
Open "c:\1\" & filename For Binary Access Write As #fnum
Put #fnum, , bytes()
Close #fnum
End Sub
and the text file version:
Code:
Option Explicit
Private Sub Form_Load()
DownLoadFromURL "http://arbital24.com/", "Test.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() = Inet1.OpenURL(ftext, icByteArray)
fnum = FreeFile
'
' Write the downloaded file to disk
'
Open "c:\1\" & filename For Binary Access Write As #fnum
Put #fnum, , bytes()
Close #fnum
End Sub
And I'm still getting a HTML-filled file:
Code:
<HTML><HEAD>
<META NAME="description" content="">
<META NAME="keywords" content="">
<TITLE></TITLE>
</HEAD>
<FRAMESET ROWS="100%,*" BORDER="0" FRAMEBORDER="0">
<FRAME SRC="http://arbital24.dyndns.org/Test.txt" SCROLLING="AUTO" NAME="bannerframe" NORESIZE>
</FRAMESET>
<NOFRAMES>
<P>
<DIV ALIGN="CENTER"><A HREF="http://arbital24.dyndns.org/Test.txt">http://arbital24.com/</A></DIV>
</NOFRAMES>
</HTML>
-
Nov 24th, 2005, 10:18 PM
#6
Thread Starter
Addicted Member
Re: Download file from Internet problems
Also, the following code:
Code:
Private Sub Command2_Click()
Dim TextIn As String
TextIn = Inet1.OpenURL("http://arbital24.com/Test.txt", icString)
MsgBox TextIn
End Sub
Generates that HTML code, as a MsgBox, so it seems to not be loading it correctly, or something
-
Nov 24th, 2005, 10:40 PM
#7
Re: Download file from Internet problems
Seems to be a problem with your ISP redirector in that they don't redirect correctly. I downloaded it manually, but the file that the program downloaded had the html for dyndns.org or something like that. Maybe you could use that url to download from? Other than that, I'm not sure how to fix it.
-
Nov 24th, 2005, 10:43 PM
#8
Thread Starter
Addicted Member
Re: Download file from Internet problems
D'oh! It *was* my DynDNS redirector. I don't know how I managed to miss that, since I have the same problem with Images: Linking to "www.arbital24.com/image.jpg" never works, I have to use the full redirector URL
Thanks for the help!
-
Nov 24th, 2005, 10:59 PM
#9
Re: Download file from Internet problems [RESOLVED]
Well, you were the first one to have problems with that code, which has been working for over a year, so I thought I'd try to figure out what was wrong.
Glad it's sorted out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|