# Downloading Image

**URL:** <https://www.ruby-forum.com/t/downloading-image/63691>\
**Category:** Ruby\
**Created:** [June 17, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/downloading-image/63691 "2006-06-17T18:29:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 17, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/downloading-image/63691/1 "2006-06-17T18:29:38Z")

</div>

Hello again

As an extension on my previous image problem I can’t even successfully  
download images from a URL location. I though tthe following simple GET  
request along with writing the result to a file would work but it  
mangles  
the image and makes it unreadable.

Net::HTTP.start(“[www.example.com](http://www.example.com)”) do |http|  
resp = http.get(“/images/logo.gif”)  
outfile = open(“C:/logo.gif”, “w”)  
outfile.write(resp.body)  
outfile.close  
end

Have I done something wrong here? Can anyone see what I’m doing wrong?

Sorry for being a pain but I’ve looked in the mailing list and various  
source files but none seem to be shedding any light so I’d be very  
grateful  
for your input.

All the best.

Doug

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 17, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/downloading-image/63691/2 "2006-06-17T19:09:19Z")

</div>

On Jun 17, 2006, at 17:27, Doug B. wrote:

> resp = http.get(“/images/logo.gif”)  
> outfile = open(“C:/logo.gif”, “w”)  
> outfile.write(resp.body)  
> outfile.close  
> end
> 
> Have I done something wrong here? Can anyone see what I’m doing  
> wrong?

On Windows you need to write certain files in ‘binary’ mode. The  
appropriate mode strings are documented in the IO class:

[http://www.ruby-doc.org/core/classes/IO.html](http://www.ruby-doc.org/core/classes/IO.html)

Also, the common idiom for writing is generally like this:

open(“somefile.etc”, ‘wb’) { |file|  
file.write(…)  
}

This is done in order to ensure that if there’s an exception after  
you open the file (ferinstance, writing when the disk is full, or in  
a place you don’t have write permissions), that the file is still  
closed. You could do the exception handling yourself, of course, but  
this is a convenient concise way of doing things.

Hope that helps,  
matthew smillie.

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 17, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/downloading-image/63691/3 "2006-06-17T19:09:37Z")

</div>

On Jun 17, 2006, at 12:27 PM, Doug B. wrote:

> resp = http.get("/images/logo.gif")  
> outfile = open(“C:/logo.gif”, “w”)  
> outfile.write(resp.body)  
> outfile.close  
> end
> 
> Have I done something wrong here? Can anyone see what I’m doing  
> wrong?

Are you on Windows? Try  
outfile = open(“C:/logo.gif”, “wb”)

You need to open the file in `binary’ mode to make it not  
automatically translate line endings.

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 17, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/downloading-image/63691/4 "2006-06-17T19:15:57Z")

</div>

You’re all so patient and helpful.

I can’t thank you enough.

Have a great weekend.

Doug

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 17, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/downloading-image/63691/5 "2006-06-17T19:09:27Z")

</div>

On 6/17/06, Doug B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> outfile.write(resp.body)  
> outfile.close  
> end
> 
> Have I done something wrong here? Can anyone see what I’m doing wrong?

As you’re running on Windows, you need to open the file in binary  
mode, ie. open(“C:/logo.gif”, “wb”).

Regards,  
Sean

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [April 20, 2009, 11:54am UTC](https://www.ruby-forum.com/t/downloading-image/63691/6 "2009-04-20T11:54:21Z")

</div>

Doug B. wrote:

> Hello again
> 
> As an extension on my previous image problem I can’t even successfully  
> download images from a URL location. I though tthe following simple GET  
> request along with writing the result to a file would work but it  
> mangles  
> the image and makes it unreadable.
> 
> Net::HTTP.start(“[www.example.com](http://www.example.com)”) do |http|  
> resp = http.get(“/images/logo.gif”)  
> outfile = open(“C:/logo.gif”, “w”)  
> outfile.write(resp.body)  
> outfile.close  
> end
> 
> Have I done something wrong here? Can anyone see what I’m doing wrong?
> 
> Sorry for being a pain but I’ve looked in the mailing list and various  
> source files but none seem to be shedding any light so I’d be very  
> grateful  
> for your input.
> 
> All the best.
> 
> Doug  
> Hi All,

Get all images from given URL

Desktop Application from jazzez

Kindly go through below link and download the EXE file.

> **[jazzez-image-v2.exe | Powered by Box](https://app.box.com/s/m5vxqpr3r1)**
>
> Get all images from the given URL

Install in Windows machine and enjoy with impressed images

Regards,  
P.Raveendran

> **[Raveendran](https://raveendran.wordpress.com/)**
>
> Nothing is impossible...
