Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
how to show an image stored in database
6 Posts
how to show an image stored in database

Actually i have came across a situation where i will have to store an image in the database and show the same image.. for this purpose i have used LONGBLOB field in mysql and stored the image.. I have to show this image to the user from db.. and i dont knwo how to show the image in this way.. I have got answer for this in php on http://www.weberdev.com/get_example-4062.html(php code.) There is some function called Header in php but i dont know about rails... Pls help me out.. Thanks and regards Saurabh Purnaye saurabh.purnaye@gmail.com

, from the top of my head - you can make a script which retrieves image data (and use this script in src attribute for your tag) The controller of the mentioned script should set "Content-Type" header, for instance, like this:

response.headers["Content-Type"] = "image/gif"

After that you just need to output image field content to stdout.

The type of course depends on image formats you store.

thanks naveen..

I handle this with an images controller:


Sr. Ruby & Rails Consultant For Hire
Sign In To Rate Post

I've had to deal with a similar problem...but since pulling large data from the database may not be very efficient (extra db load, pulling data over the network not to mention that your app server (i.e. Mongrel) now has to serve them where previously nginx/apache could have), I found a good strategy is to cache images from the database to the standard location in the filesystem (in public/images) and let your webserver serve them in the traditional manner.

What I do is cache all images on application startup and then cache them again if they are modified. In order to do this I created an fs_dump! method on the class that encapsulates image assets:


I made something similar but with PDF files. PDF files are stored on database using BFILEs. I made a method which gets them, make a local copy and open acrobat reader...

6 Posts
Login to add your message