You will need to add an Inet control (Microsoft Internet Transfer Control) to the form.
Code:
Private Sub Command1_Click()
Dim Size As Long, Remaining As Long, FFile As Integer, Chunk() As Byte
Inet1.Execute "http://www.codeguru.com/forum/images/cg-logo.gif", "GET"
Do While Inet1.StillExecuting
DoEvents
Loop
Size = CLng(Inet1.GetHeader("Content-Length"))
Remaining = Size
FFile = FreeFile
Open "c:\logo.gif" For Binary Access Write As #FFile
Do Until Remaining = 0
If Remaining > 1024 Then
Chunk = Inet1.GetChunk(1024, icByteArray)
Remaining = Remaining - 1024
Else
Chunk = Inet1.GetChunk(Remaining, icByteArray)
Remaining = 0
End If
Put #FFile, , Chunk
Loop
Close #FFile
MsgBox "File downloaded"
End Sub
[Cimperiali colorized for better reading]
Last edited by Cimperiali; December 7th, 2002 at 05:42 AM.
I try it and it work but the file size is not the right size. I get 4166 but the right size should be 564,230 why???
and what is the meaning of 1024 in:
If Remaining > 1024 Then
Chunk = Inet1.GetChunk(1024, icByteArray)
Remaining = Remaining - 1024
Else
Chunk = Inet1.GetChunk(Remaining, icByteArray)
Remaining = 0
End If
About the size, that's very strange, it should be correct. What url are you trying to download?
About the 1024, this is just a number. With this, I'm saying that I want to get the file in chunks of 1024 bytes. You can change this number if you want to make the size larger/smaller.
This is usefull for when you decide to show a progress bar or something like that.
Say you have a file of 5000 bytes, and a blocksize of 1024. You will traverse the loop 5 times.
However, if you set the blocksize to 8096, you will only go through the loop once, but your application will appear frozen until the 8096 bytes are retrieved.
1024 is quite slowe, but of course it depends on your internet connection. If you only have a 56K connection, the 8096 block will freeze the application for over a second( in optimal conditions), where the 1024 block size probably doesnt. If you are working over a lan (or have a broadband connection), you probably won't notice a difference between the two. Anyway, these number can be any (if you want 9999, it is 9999), I just like working with powers of 2.
I need some command to stop / cancel the file download process. When I exit the program during download, I get "runtime error 35763", and it tellms me that the line
Chunk = Inet1.GetChunk(Remaining, icByteArray)
has an invalid argument. I think the problem is in the Remaining variable, becasue also annoys a status bar, because the program tries to assign the statbar an invalid value.
So, how can I kill this process without problem, with a button click event for example?
I do believe that the Inet control holds a method to stop processing. I'm not exactly sure what it is called (dont have PC to check here), but I assume it will be something like Inet1.Cancel or Inet1.Stop. You will need to trap the error you will get in the loop that's handling the download, a simple error handler should do the job.
Cakkie,
Cant tell you how much you have helped me, without you even knowing it! Thanks!
I have used this routine to accomplish a major portion of what I was planning to do with a new application.
The situation now is, I would like to download a complete folder. Is it possible to tweak this code any to accomplish this? I would like to be able to use the same routine for everything in the app.
Thanks again, your previous posts have answered SOOO many questions....without having to post!
The secret of life, is enjoying the passage of time. - James Taylor
I m trying to include this in my Code right now..with a pdf file and it says Type mistmach... any idea?
Can we specify where to download it or should i use another command?
Thanks!
Last edited by Kain; August 21st, 2003 at 11:19 AM.
---------------------------------------------
Sure i got it... what s a form again?
hi all...i've found this code..and it works very well!!
but i've got a BIG problem...
i'll tried to download a text file:
after i've downloaded the txt file...when i open this one, all the things that are wrote in the text file appear on an only line, all the Return key doesn't exist !!!
but if i open it with another apllication that read the text files, all the words wrote in the file appear every one in a different line (and this is right!).
what can i do to download a text file a have the file like this: ?
Cakkie.. i wouldnt use this method.. (a tight loop on DoEvents)
for all those here who are having trouble with this task.. i ahve written a wrapper for the Inet control in oprder to add a progress bar, and as a transfer client it works acceptably for HTTP. i have not tried ftp.
it downloads both text and binary acceptably.. for some reason cakkie's provision does not, and i have seen other code on the web, that uses inet, that also mangles data.
cakkie; why do you not use the events that inet fires?
please note that this project is currently in development by myself, and i cannot guarantee that it is finished!
i want to implement multiple files queuing and event emission when finished. as this is part of a commercial project, this may be some time soon but it cannot be guaranteed
Last edited by cjard; November 24th, 2003 at 01:35 PM.
note: inet, as a control, sucks.. theres not much i can do about that.. and this tool does get annoyed if you start and cancel start and cancel over and over..
sometimes it tells you that the downlaod completed (because thats the event that inet fires)
sometimes it cancels as it should
and sometimes it tells you: error
note the example form i knocked together to test some bits out.. have a look at the functions/subs that PD exposes if you wanna fiddle
Originally posted by Cakkie
You will need to add an Inet control (Microsoft Internet Transfer Control) to the form.
Code:
Private Sub Command1_Click()
Dim Size As Long, Remaining As Long, FFile As Integer, Chunk() As Byte
Inet1.Execute "http://www.codeguru.com/forum/images/cg-logo.gif", "GET"
Do While Inet1.StillExecuting
DoEvents
Loop
Size = CLng(Inet1.GetHeader("Content-Length"))
Remaining = Size
FFile = FreeFile
Open "c:\logo.gif" For Binary Access Write As #FFile
Do Until Remaining = 0
If Remaining > 1024 Then
Chunk = Inet1.GetChunk(1024, icByteArray)
Remaining = Remaining - 1024
Else
Chunk = Inet1.GetChunk(Remaining, icByteArray)
Remaining = 0
End If
Put #FFile, , Chunk
Loop
Close #FFile
MsgBox "File downloaded"
End Sub
[Cimperiali colorized for better reading]
thanks alot. this code was great, but what i need is to put a string in place of the URL. can anyone help me with this?
ie. i have replaced:
Inet1.Execute "http://www.codeguru.com/forum/images/cg-logo.gif", "GET"
ive attached a module from an old project, called ProgressDownloader, you basically make a new instance of it (it is a class module) and tell it to report its progress with a certain progress bar, or you can use a timer to repeatedly ask it for its progress, and tell it to download something. the interesting code is in Public Sub downloadUrlTo(url, filename):
Originally posted by cjard
no idea. it works for me.
ive attached a module from an old project, called ProgressDownloader, you basically make a new instance of it (it is a class module) and tell it to report its progress with a certain progress bar, or you can use a timer to repeatedly ask it for its progress, and tell it to download something. the interesting code is in Public Sub downloadUrlTo(url, filename):
woha backup buddy that's a little too intense for me! what do you mean it works for you? can you please append this code for me to how you say it works, or show me a better way that i can download files from a site? i'm really confused:
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.