# Upload image to MySQL

**URL:** <https://www.ruby-forum.com/t/upload-image-to-mysql/163162>\
**Category:** Rails\
**Created:** [March 17, 2009, 9:37pm UTC](https://www.ruby-forum.com/t/upload-image-to-mysql/163162 "2009-03-17T21:37:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 17, 2009, 9:37pm UTC](https://www.ruby-forum.com/t/upload-image-to-mysql/163162/1 "2009-03-17T21:37:19Z")

</div>

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

Rails 2.2.2

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 17, 2009, 9:52pm UTC](https://www.ruby-forum.com/t/upload-image-to-mysql/163162/2 "2009-03-17T21:52:53Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 17, 2009, 10:32pm UTC](https://www.ruby-forum.com/t/upload-image-to-mysql/163162/3 "2009-03-17T22:32:09Z")

</div>

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
