# Downloading files

**URL:** <https://www.ruby-forum.com/t/downloading-files/147586>\
**Category:** Ruby\
**Created:** [September 3, 2008, 1:03pm UTC](https://www.ruby-forum.com/t/downloading-files/147586 "2008-09-03T13:03:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [September 3, 2008, 1:03pm UTC](https://www.ruby-forum.com/t/downloading-files/147586/1 "2008-09-03T13:03:43Z")

</div>

Hi,

Which is the proper way to download a binary file?

Trying:  
open(temp\_path,“w”).write(open(http\_path).read)

returns truncated files

Cheers,  
M.

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [September 3, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/downloading-files/147586/2 "2008-09-03T15:28:03Z")

</div>

Marcelo B. wrote:

> M.

Are you on Windows? Try opening the output file as “wb” instead of just  
“w”.

Here’s a snippet from my RMagick OS X installer script. Ara Howard gave  
me the code. ‘uri’ is the input URI, something like  
“[http://rubyforge.org/frs/download.php/39890/RMagick-2.5.2.tar.gz](http://rubyforge.org/frs/download.php/39890/RMagick-2.5.2.tar.gz)”

```
   begin
     open(uri) do |fin|
       open(File.basename(uri), "w") do |fout|
         while (buf = fin.read(8192))
           fout.write buf
         end
       end
     end
   rescue Timeout::Error
     abort <<-END_HTTP_DL_TIMEOUT
     A timeout occurred while downloading #{uri}.
     Probably this is a temporary problem with the host.
     Try again later.
     END_HTTP_DL_TIMEOUT
   end

```

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [September 3, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/downloading-files/147586/3 "2008-09-03T15:34:05Z")

</div>

Marcelo B. wrote:

> Hi,
> 
> Which is the proper way to download a binary file?
> 
> Trying:  
> open(temp\_path,“w”).write(open(http\_path).read)
> 
> returns truncated files
> 
> Cheers,  
> M.

here is a snippet that should work :

require “rubygems”  
require “mechanize”

agent = WWW::Mechanize.new  
agent.get(link\_to\_download).save\_as(“name\_of\_the\_file”)

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [September 3, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/downloading-files/147586/4 "2008-09-03T15:43:05Z")

</div>

Marcelo B. wrote:

> Hi,
> 
> Which is the proper way to download a binary file?
> 
> Trying:  
> open(temp\_path,“w”).write(open(http\_path).read)
> 
> returns truncated files
> 
> Cheers,  
> M.

Without Mechanize, it’s in fact better to do it in two lines:

data=open(remote).read  
File::open(local,“wb”){|f| f\<\<data}

Because now you don’t create an empty file in case of net error.

> **[Downloading a file](http://al2o3-cr.blogspot.com/2008/08/downloading-file.html)**
>
> Today we will download a Garfield comic strip. We will not display it, as displaying is a whole lot harder, it needs a window and so on, may...

TPR.
