# HTTPClient download binary file

**URL:** <https://rubytalk.org/t/httpclient-download-binary-file/46256>\
**Category:** ruby-talk\
**Created:** [2 May 2008 20:22 UTC](https://rubytalk.org/t/httpclient-download-binary-file/46256 "2008-05-02T20:22:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aa\_Aa](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@Aa\_Aa](https://rubytalk.org/u/Aa_Aa)\
**Post date:** [2 May 2008 20:22 UTC](https://rubytalk.org/t/httpclient-download-binary-file/46256/1 "2008-05-02T20:22:28Z")

</div>

Hi,

I'm writing a simple program which downloads a resource from the  
internet. When I save a file to disk, it always ends up a few bytes  
larger than it should be and is thus recognized as a damaged file.

The code I'm using:

require 'httpclient'  
client = HTTPClient.new(nil)  
my\_file = File.new("x:\\temp\\logo2.gif", "w")  
puts  
my\_file.write(client.get\_content("[http://www.google.com/intl/en\_ALL/images/logo.gif](http://www.google.com/intl/en_ALL/images/logo.gif)"))

puts actually prints the correct size 8558, but when I check the size on  
disk it is 8617 bytes.  
I've also downloaded the file with firefox to check if the increased  
file size is due to the filesystem i'm using. When downloaded which  
firefox the file is 8558 bytes on disk, so something must be wrong in my  
code.

Regards,  
DarnDao

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Heesob\_Park](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@Heesob\_Park](https://rubytalk.org/u/Heesob_Park)\
**Post date:** [3 May 2008 00:31 UTC](https://rubytalk.org/t/httpclient-download-binary-file/46256/2 "2008-05-03T00:31:29Z")

</div>

Hi,

Aa Aa wrote:

> Hi,
> 
> I'm writing a simple program which downloads a resource from the  
> internet. When I save a file to disk, it always ends up a few bytes  
> larger than it should be and is thus recognized as a damaged file.
> 
> The code I'm using:
> 
> require 'httpclient'  
> client = HTTPClient.new(nil)  
> my\_file = File.new("x:\\temp\\logo2.gif", "w")

try with:  
my\_file = File.new("x:\\temp\\logo2.gif", "wb")

> puts  
> my\_file.write(client.get\_content("[http://www.google.com/intl/en\_ALL/images/logo.gif&quot;\](http://www.google.com/intl/en_ALL/images/logo.gif&quot;%5C)))
> 
> puts actually prints the correct size 8558, but when I check the size on  
> disk it is 8617 bytes.  
> I've also downloaded the file with firefox to check if the increased  
> file size is due to the filesystem i'm using. When downloaded which  
> firefox the file is 8558 bytes on disk, so something must be wrong in my  
> code.
> 
> Regards,  
> DarnDao

Regards,  
Park Heesob

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Aa\_Aa](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@Aa\_Aa](https://rubytalk.org/u/Aa_Aa)\
**Post date:** [3 May 2008 01:19 UTC](https://rubytalk.org/t/httpclient-download-binary-file/46256/3 "2008-05-03T01:19:58Z")

</div>

Thanks works ok now. Since the b parameter is DOS\Windows only, would  
that throw an exception on a non windows OS?

Heesob Park wrote:

> **···**
>
> > Hi,
> > 
> > Aa Aa wrote:
> > 
> > > Hi,
> > > 
> > > I'm writing a simple program which downloads a resource from the  
> > > internet. When I save a file to disk, it always ends up a few bytes  
> > > larger than it should be and is thus recognized as a damaged file.
> > > 
> > > The code I'm using:
> > > 
> > > require 'httpclient'  
> > > client = HTTPClient.new(nil)  
> > > my\_file = File.new("x:\\temp\\logo2.gif", "w")
> > 
> > try with:  
> > my\_file = File.new("x:\\temp\\logo2.gif", "wb")
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
