Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert how to show an image stored in...

Replytotopic

how to show an image stored in database

Posted in Forums : Ask a Rails expert

 
Dsc00495-medium;brt:55

Authority 37
Posting Rating 69
Sign in to rate this post

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

 
Profile

Authority 62
Posting Rating 97
Sign in to rate this post

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

 
Dsc00495-medium;brt:55

Authority 37
Posting Rating 69
Sign in to rate this post

thanks naveen..

 
Rj-blogshot

Authority 75
Posting Rating 95
Sign in to rate this post

I handle this with an images controller:

http://pastie.org/161189

 
2350119259_c0f090bac0_m

Authority 62
Posting Rating 44
Sign in to rate this 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:

http://pastie.caboo.se/162364

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

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…

Replytotopic

Other Recent Topics

Ask a Rails expert : How to use mephisto

Ask a Rails expert : How to use mephisto

Ask a Rails expert : will_paginate customization problem

Ask a Rails expert : BackgroundRB still wants 'development' environment...?

Ask a Rails expert : activescaffold, sql exception

Ask a Rails expert : Passing non-english chars in query string

Ask a Rails expert : Rails and 2D barcodes

Ask a Rails expert : apache giving proxy error

Ask a Rails expert : Custom Responses w/ 'extra' information...?

Ask a Rails expert : Log rotation in rails

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel