# Upload image to MySQL

**URL:** <https://discuss.rubyonrails.org/t/upload-image-to-mysql/40546>\
**Category:** rubyonrails-talk\
**Created:** [March 17, 2009, 8:37pm UTC](https://discuss.rubyonrails.org/t/upload-image-to-mysql/40546 "2009-03-17T20:37:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 17, 2009, 8:37pm UTC](https://discuss.rubyonrails.org/t/upload-image-to-mysql/40546/1 "2009-03-17T20: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:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [March 17, 2009, 8:51pm UTC](https://discuss.rubyonrails.org/t/upload-image-to-mysql/40546/2 "2009-03-17T20:51:54Z")

</div>

Fresh Mix 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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 17, 2009, 9:32pm UTC](https://discuss.rubyonrails.org/t/upload-image-to-mysql/40546/3 "2009-03-17T21: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', &nbsp;&nbsp;:account\_id =\> @account.id}, :update =\> 'image\_div' ) do -%\>

&nbsp;&nbsp;\<p\>\<label for="image\_img"\>Select a File:\</label\> &nbsp;&nbsp;\<%= file\_field 'image', 'img' %\>\</p\> &nbsp;&nbsp;\<%= submit\_tag "Upload" %\> \<% end -%\>

:controller &nbsp;&nbsp;@image = Image.new &nbsp;&nbsp;@image.img = File.open(params[:image][:img] , "rb").read &nbsp;&nbsp;@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 &nbsp;&nbsp;\<%= 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
