Writing an image to file


#1

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


#2

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


#3

Yep, that worked. Thank you very much.

/Marcus

Matthew M. skrev:


#4

Hi All,

Get all images from given URL

Desktop Application from jazzez

Kindly go through below link and download the EXE file.

http://www.box.net/shared/m5vxqpr3r1

Install in Windows machine and enjoy with impressed images

Regards,
P.Raveendran
http://raveendran.wordpress.com