How to insert an image into database and how to display it

Hi all,

I need to insert an image file into the oracle database and want to
display the image in the view.for that i had written the following code.

In the controller

def image_view
@image = Imageresult.find(params[:sel_id])
send_data @image.image_field,
:filename => “#{@image.application.name.strip}.png”,
:content_type => ‘image/png’,
:disposition => ‘inline’
end

For Inserting image file in to the DB.

i=Imageresult.create(:id=>1,:image_field=>File.read(’/public/images/img/new_img.png’))

i.save

And for display of image in the view i’m calling the function
image_view when clicked on a link(through javascript), every thing is
fine.
But instead of displaying the image it show binary data which is stored
in the database.

Is anything wrong with the content type or some thing else?

Thanks
VK.

Rather than put the image file into the database, it’s better to save it
in your filesystem, and store the path to it (relative to your public
folder) in your database. Then when you want to display it you can just
say something like

image_tag(image.path)

There’s lots of plugins/gems to simplify this process, and wrap various
safeguards and functionality around it. attachment_fu is a popular one.

Max W. wrote:

Rather than put the image file into the database, it’s better to save it
in your filesystem, and store the path to it (relative to your public
folder) in your database. Then when you want to display it you can just
say something like

+1

There’s lots of plugins/gems to simplify this process, and wrap various
safeguards and functionality around it. attachment_fu is a popular one.

From what I’ve seen, it seems that Paperclip has become the popular
choice:

Vamsi K. wrote:

Max W. wrote:

Rather than put the image file into the database, it’s better to save it
in your filesystem, and store the path to it (relative to your public
folder) in your database. Then when you want to display it you can just
say something like

image_tag(image.path)

There’s lots of plugins/gems to simplify this process, and wrap various
safeguards and functionality around it. attachment_fu is a popular one.

Williams, i used to do what you said, but based on clients’ requirement
i’ve to save the image file in to the database.

Then your client’s requirement is unreasonable. Images don’t belong in
the DB, and you should tell your client this.

for that i had written
the above code and while displaying the image it shows the binary code
instead of image.

i’ve been struggling to accomplish this from couple of days…

Thanks
VK.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi

Then your client’s requirement is unreasonable. Images don’t belong in
the DB, and you should tell your client this.

But if the images are some logos,etc and also image size is very 

minimum ,I think there is no harm in storing them in db

Sijo

Hi Vamsi K.

def image_view
@image = Imageresult.find(params[:sel_id])
send_data @image.image_field,
:filename => “#{@image.application.name.strip}.png”,
:content_type => ‘image/png’,
:disposition => ‘inline’
end

  Are you trying to show the image in the page? Then you can try

<%= image_tag url_for(:controller => “controller_name”, :action =>
“image_view”,:sel_id => image.id) %>

   You have to pass the image id according to your idea here

Sijo

Max W. wrote:

Rather than put the image file into the database, it’s better to save it
in your filesystem, and store the path to it (relative to your public
folder) in your database. Then when you want to display it you can just
say something like

image_tag(image.path)

There’s lots of plugins/gems to simplify this process, and wrap various
safeguards and functionality around it. attachment_fu is a popular one.

Williams, i used to do what you said, but based on clients’ requirement
i’ve to save the image file in to the database.for that i had written
the above code and while displaying the image it shows the binary code
instead of image.

i’ve been struggling to accomplish this from couple of days…

Thanks
VK.

Sijo k g wrote:

Hi

Then your client’s requirement is unreasonable. Images don’t belong in
the DB, and you should tell your client this.

But if the images are some logos,etc and also image size is very 

minimum ,I think there is no harm in storing them in db

If it’s a small number of logos, they go in the filesystem.

I can imagine that an object DB would do well with images in it, but
AFAIK, SQL DBs tend not to like that.

Sijo

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi

Saving in db or filesyetm, each have its own pros and cons and is
debatable. What about storing in db and cache them on disk and sweep
them when necessary. Anyway again this for only small number of images
of small size say some kb

Sijo

But if the images are some logos,etc and also image size is very 

minimum ,I think there is no harm in storing them in db

Sijo

There are lots of disadvantages to storing them in the database, and no
advantages.

The web server will serve images MUCH more efficiently from the file
system than the database, for a lot of different reasons. So, storing
them in the database will slow down your website. That counts as ‘harm’
as far as i’m concerned.

Saying that ‘they’re only little’ isn’t a reason to do something wrong.

Marnen Laibow-Koser wrote:

Williams, i used to do what you said, but based on clients’ requirement
i’ve to save the image file in to the database.

Then your client’s requirement is unreasonable. Images don’t belong in
the DB, and you should tell your client this.

I know of one company that sells healthcare software which uses a
database to store scanned documents. The logic of it escapes me, but I
can’t deny that the documents came up really fast when they were
requested. I also can’t deny that after 3-4 years, the database for the
images was about 1.8 terabytes.

Sijo k g wrote:

Hi

Saving in db or filesyetm, each have its own pros and cons and is
debatable. What about storing in db and cache them on disk and sweep
them when necessary. Anyway again this for only small number of images
of small size say some kb

Sijo

That debate has been decided in favour of the filesystem, by many many
people who are much smarter and more experienced than you or I. It’s
both EASIER TO IMPLEMENT and MORE EFFICIENT. It’s not always the case
that those two factors are combined in a single solution, so whenever it
happens you should go for it.

Your suggestion of doing both is much more complicated than simply
keeping them in the filesystem, and is basically asking for trouble.

If you say to your client “We store all of the information about the
files in the database, but for reasons of performance and reliability
the actual file data is stored on the server” are they really going to
insist that you put them into the database?