Download a file piece by piece

P

Patrick Plattes

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
 
L

Louis J Scoras

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.
 
P

Patrick Plattes

Louis said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top