CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

    Question How to download a file from the internet

    HI ALL
    I want to give the option in my application to download updates.
    How can i download a file with VB code ???

  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477
    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.
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  3. #3
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396
    First many thanks...

    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

  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477
    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.
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  5. #5
    Join Date
    Feb 2003
    Location
    AR
    Posts
    228
    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?

  6. #6
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477
    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.
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  7. #7
    Join Date
    Apr 2003
    Posts
    53
    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

  8. #8
    Join Date
    Apr 2003
    Posts
    53

    Inet download (cont'd)

    Bump.
    The secret of life, is enjoying the passage of time. - James Taylor

  9. #9
    Join Date
    Apr 2001
    Location
    USA
    Posts
    161
    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?

  10. #10
    Join Date
    Nov 2003
    Posts
    5
    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: ?

    Right EX:

    Alex
    Mark
    Frank
    Sara

    Wrong EX:

    AlexMarkFrankSara


    tnx at all

  11. #11
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    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
    Attached Files Attached Files
    Last edited by cjard; November 24th, 2003 at 01:35 PM.
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  12. #12
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  13. #13
    Join Date
    Mar 2004
    Posts
    30
    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"


    with:


    Stringname = "http://www.codeguru.com/forum/images/cg-logo.gif"
    Inet1.Execute Stringname, "GET"


    when i've done this it has said URL is Malformed. why is this? can somebody please tell me how to replace the url with a string?
    Last edited by Colt_777; April 26th, 2004 at 10:15 PM.

  14. #14
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    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):
    Attached Files Attached Files
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  15. #15
    Join Date
    Mar 2004
    Posts
    30
    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:

    Inet1.Execute urlstring, "GET"

    thanx.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured