# Writing an image to file

**URL:** <https://www.ruby-forum.com/t/writing-an-image-to-file/53519>\
**Category:** Ruby\
**Created:** [February 8, 2006, 7:00am UTC](https://www.ruby-forum.com/t/writing-an-image-to-file/53519 "2006-02-08T07:00:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@marcus](https://www.ruby-forum.com/u/marcus)\
**Post date:** [February 8, 2006, 7:00am UTC](https://www.ruby-forum.com/t/writing-an-image-to-file/53519/1 "2006-02-08T07:00:21Z")

</div>

I have images stored in a database. I now need to read those images from  
the database and store to files (using ActiveRecord in a system where  
$KCODE = ‘u’; require ‘jcode’ is used (if that matters…)).

I’ve tried a couple of approaches but it’s only giving me destroyed  
images (when served to a browser using webrick they look perfectly  
alright).

I’ve tried:

```
 str_bin = ""
 self.data.each_byte do |b|
   str_bin << b
 end
 File.open("public/published/#{self.name}", "w") do |file|
   file.print str_bin
 end

```

and

```
 File.open("public/published/#{self.name}", "w") do |file|
   file.syswrite self.data
 end

```

and

```
 File.open("public/published/#{self.name}", "w") do |file|
   file.print self.data
 end

```

All with the exact same result.

I suppose there must be a simple way to solve this?

Regards

/Marcus

---

<div class="post-metadata">

**Author:** ![marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@marcus](https://www.ruby-forum.com/u/marcus)\
**Post date:** [February 8, 2006, 7:06am UTC](https://www.ruby-forum.com/t/writing-an-image-to-file/53519/2 "2006-02-08T07:06:25Z")

</div>

Does using “wb” instead of “w” in File.open make a difference?

---

<div class="post-metadata">

**Author:** ![marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@marcus](https://www.ruby-forum.com/u/marcus)\
**Post date:** [February 8, 2006, 7:06am UTC](https://www.ruby-forum.com/t/writing-an-image-to-file/53519/3 "2006-02-08T07:06:26Z")

</div>

Yep, that worked. Thank you very much.

/Marcus

Matthew M. skrev:

---

<div class="post-metadata">

**Author:** ![marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@marcus](https://www.ruby-forum.com/u/marcus)\
**Post date:** [April 20, 2009, 11:55am UTC](https://www.ruby-forum.com/t/writing-an-image-to-file/53519/4 "2009-04-20T11:55:29Z")

</div>

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