Download a file piece by piece

Discussion in 'Ruby' started by Patrick Plattes, Nov 30, 2006.

  1. Hello people :),

    i have a question about downloading files. I want to download a file and
    print out how many bytes are downloaded. That's important to me, because
    i want to have a progress bar later.

    The problem is that it looks like my code downloads the whole file and
    after that it executes the while loop - this behavior is useless for me.

    does anyone know how to fix it or knows a better solution?

    Thanks,
    Patrick

    A working example:

    require 'open-uri'

    FILE_BUFFER_SIZE = 65536
    filename = "dailysourcecode-39523-11-30-2006.mp3"
    url =
    "http://m-uk.podshow.com/media/21/episodes/39523/dailysourcecode-39523-11-30-2006.mp3"

    open(url, "r") do |input|
    open(filename, "w") do |output|
    bytes_downloaded = 0
    while (buffer = input.read(FILE_BUFFER_SIZE))
    output.write(buffer)
    bytes_downloaded += FILE_BUFFER_SIZE
    puts bytes_downloaded
    end
    end
    end
     
    Patrick Plattes, Nov 30, 2006
    #1
    1. Advertising

  2. On 11/30/06, Patrick Plattes <> wrote:

    > i have a question about downloading files. I want to download a file and
    > print out how many bytes are downloaded. That's important to me, because
    > i want to have a progress bar later.


    > The problem is that it looks like my code downloads the whole file and
    > after that it executes the while loop - this behavior is useless for me.


    It would be nice if there was an easier way to handle the data as it
    comes in; but if you just need to do a progress bar, Kernel.open takes
    two optional parameters just for this purpose:

    :content_length_proc - gets called initially w/ the document size, if it
    could be obtained in the content-length header

    :progress_proc - gets called with the size of the data read so far.

    It's in the rdoc for open-uri under OpenURI::OpenRead#open.


    --
    Lou.
     
    Louis J Scoras, Nov 30, 2006
    #2
    1. Advertising

  3. Louis J Scoras schrieb:
    > It would be nice if there was an easier way to handle the data as it
    > comes in; but if you just need to do a progress bar, Kernel.open takes
    > two optional parameters just for this purpose:
    >
    > :content_length_proc - gets called initially w/ the document size, if it
    > could be obtained in the content-length header
    >
    > :progress_proc - gets called with the size of the data read so far.
    >
    > It's in the rdoc for open-uri under OpenURI::OpenRead#open.
    >


    Thanks! I'm happy. It works really great :)

    Have a nice day,
    Patrick
     
    Patrick Plattes, Nov 30, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Taylor
    Replies:
    2
    Views:
    974
    Ryan Taylor
    Nov 10, 2004
  2. Steve C. Orr [MVP, MCSD]
    Replies:
    0
    Views:
    1,637
    Steve C. Orr [MVP, MCSD]
    Mar 7, 2005
  3. Sam --
    Replies:
    2
    Views:
    633
    Sam --
    Mar 17, 2005
  4. Brett  Kelly
    Replies:
    1
    Views:
    692
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  5. vishnusreenath

    To add a piece of code from a .c file to a .h file

    vishnusreenath, Jul 5, 2012, in forum: C Programming
    Replies:
    0
    Views:
    379
    vishnusreenath
    Jul 5, 2012
Loading...

Share This Page