Upload image to MySQL


#1

Need simple sample, how to upload image in to MySQL database (in BLOB)

Rails 2.2.2


#2

Fresh M. wrote:

Need simple sample, how to upload image in to MySQL database (in BLOB)

That’s one of the big “Don’ts!” among the “Do’s and Don’t’s of
Websites”. There
are many, many reasons not to put images into your database. The very
least of
which - your web server cannot bypass Rails and efficiently serve the
image!

Use Paperclip - it’s the best image uploader/storer/server plugin we
have seen
so far…


#3

your web server cannot bypass Rails and efficiently serve the

image!

I admit that I am by no means a database expert but I implemented this
about two weeks ago and have had no problems (so far).

Here is what I did.

First my field in the adatabase I declared as a bytea (Byte Array). I
tried using other field types, such as OID, with no success.

View:
<% form_remote_tag( :url => {:action => ‘uploadFile’,
:account_id => @account.id}, :update => ‘image_div’ ) do -%>

Select a File: <%= file_field 'image', 'img' %>

<%= submit_tag "Upload" %> <% end -%>

:controller
@image = Image.new
@image.img = File.open(params[:image][:img] , “rb”).read
@image.save

I am not sure what purpose the “rb” serves since I had to look long and
hard for a working example, and in the end I think I was relegated to
trying different approaching from several examples.

:View to render the image - First get image via simple find
<%= image_tag url_for(:action => ‘image’, :account_id => @account.id)
%>

Like I said this example renders the image just fine, but I would be
very interested to hear what possible problems there are with this
appraoch as I have done this for a project at work and it is about a
month away from going live. Thanks,

-S