# Trying to download first 10k

**URL:** <https://www.ruby-forum.com/t/trying-to-download-first-10k/121874>\
**Category:** Ruby\
**Created:** [December 2, 2007, 9:22am UTC](https://www.ruby-forum.com/t/trying-to-download-first-10k/121874 "2007-12-02T09:22:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 2, 2007, 9:22am UTC](https://www.ruby-forum.com/t/trying-to-download-first-10k/121874/1 "2007-12-02T09:22:46Z")

</div>

Hi, I’m trying to download via http only the first, say, 10k of a file.  
I’m using open-uri but even when I try to break out of the core loop  
where it’s reading the buffer, I can hear my hard-drive grinding away  
downloading the entire 3MB. When I look at the file written to the disk  
it shows only 10k, but judging by the length of time it takes to execute  
the script and the grinding sound of my hard-drive, I think it’s  
downloading the entire file.

I read the other posts about a progress bar, but that’s not really what  
I’m trying to do… or is it?

Is there a way to interrupt the download? As you can see I tried to  
break out of the code blocks every chance I got.

Here is my code:

def download\_first\_n\_kilobytes n, file\_name  
n = 1024 \* n  
uri = file\_name  
count = 0  
open(uri) do |fin|  
open(convert\_to\_file\_name(uri), ‘w’) do |fout|  
while(buf = fin.read(n))  
count += 1  
fout.write buf  
if count \> 0  
break  
end # if  
end # while  
break  
end # open  
break  
end # open  
end # def

def convert\_to\_file\_name uri  
“mp3/” + File.basename(uri).to\_s  
end

uri = “[http://www.sapht.com/music/tmp/sapht\_-\_alvaros\_messenger.mp3](http://www.sapht.com/music/tmp/sapht_-_alvaros_messenger.mp3)”  
download\_first\_n\_kilobytes 10, uri

Thanks a lot for looking at this!

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 2, 2007, 9:26am UTC](https://www.ruby-forum.com/t/trying-to-download-first-10k/121874/2 "2007-12-02T09:26:19Z")

</div>

Oh yeah, at the top there should be:

require ‘open-uri’

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 2, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/trying-to-download-first-10k/121874/3 "2007-12-02T13:14:38Z")

</div>

On Dec 2, 2007 10:22 AM, David B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> open(convert_to_file_name(uri), 'w') do |fout|
> 
> ```
> 
> end # open  
> end # def
> 
> def convert\_to\_file\_name uri  
> “mp3/” + File.basename(uri).to\_s  
> end
> 
> uri = “[http://www.sapht.com/music/tmp/sapht\_-\_alvaros\_messenger.mp3](http://www.sapht.com/music/tmp/sapht_-_alvaros_messenger.mp3)”  
> download\_first\_n\_kilobytes 10, uri

Looking at the documentation for open-uri at  
[http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/), it seems to be  
that it is default open-uri behavior to download the whole file and  
just then give you control. You might succeed if you give a  
:progress\_proc that read()s and close()s if size\>n, but I doubt it  
would work.

Otherwise, use a more powerful way of downloading files, like  
Net::HTTP, with something like  
s = 0  
http.request\_get(‘/index.html’) {|res|  
res.read\_body do |segment|  
s += segment.size  
print segment  
res.finish if s \> n  
end  
}

See the docs at  
[http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html)

Aur

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 2, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/trying-to-download-first-10k/121874/4 "2007-12-02T23:30:13Z")

</div>

Quoth David B.:

> ```
> open(convert_to_file_name(uri), 'w') do |fout|
> 
> ```
> 
> end # open  
> Thanks a lot for looking at this!  
> You’ll probably not want to be using open-uri but instead a custom HTTP  
> query  
> with a “Range: bytes=0-10239” header.

HTH,
