# Trying to upload image in database

**URL:** <https://www.ruby-forum.com/t/trying-to-upload-image-in-database/117684>\
**Category:** Rails\
**Created:** [October 19, 2007, 11:42am UTC](https://www.ruby-forum.com/t/trying-to-upload-image-in-database/117684 "2007-10-19T11:42:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![priyasaini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@priyasaini](https://www.ruby-forum.com/u/priyasaini)\
**Post date:** [October 19, 2007, 11:42am UTC](https://www.ruby-forum.com/t/trying-to-upload-image-in-database/117684/1 "2007-10-19T11:42:21Z")

</div>

Hi All,

I got the Error message when i try to upload a image file into database  
using Ruby on Rails ,

PGError: ERROR: unterminated quoted string at or near “'ï¿½ï¿½ï¿½ï¿½”  
LINE 1: …O photos (“description”, “picture”) VALUES(‘flower’, 'ï¿½ï¿½ï¿½ï¿½  
^  
: INSERT INTO photos (“description”, “picture”) VALUES(‘flower’,  
'ï¿½ï¿½ï¿½ï¿½ï¿½JFIFï¿½ï¿½`ï¿½`ï¿½ï¿½ï¿½ï¿½ï¿½Cï¿½  
aaa  
  
…  
…  
…  
…  
…  
…

#%$“”!&+7/&)4)!"0A149;\>\>\>%.DIC\<H7=\>;ï¿½ï¿½ï¿½C’)

I am using  
DataBase :Postgresql 8.2 version  
OS : windowsXp  
Rails : version 1.2.4 and server : mongrel

If anybody could know the solution for above problem, let me know as  
soon as possible.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![priyasaini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@priyasaini](https://www.ruby-forum.com/u/priyasaini)\
**Post date:** [October 19, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/trying-to-upload-image-in-database/117684/2 "2007-10-19T16:57:26Z")

</div>

I would recommend storing them in base64 format.

require ‘base64’

ascii = Base64::encode64(binary)

binary = Base64::decode64(ascii)

You could do this in a filter of course. I just made a dummy  
attribute called “image\_binary” which, upon setting, base64 encodes  
the data, and when reading base64 decodes it.

–Michael

---

<div class="post-metadata">

**Author:** ![priyasaini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@priyasaini](https://www.ruby-forum.com/u/priyasaini)\
**Post date:** [October 22, 2007, 8:14am UTC](https://www.ruby-forum.com/t/trying-to-upload-image-in-database/117684/3 "2007-10-22T08:14:17Z")

</div>

Michael G. wrote:

> I would recommend storing them in base64 format.
> 
> require ‘base64’
> 
> ascii = Base64::encode64(binary)
> 
> binary = Base64::decode64(ascii)
> 
> You could do this in a filter of course. I just made a dummy  
> attribute called “image\_binary” which, upon setting, base64 encodes  
> the data, and when reading base64 decodes it.
> 
> –Michael

This is my RHTML code to upload image path from user:

Picture

\<%= file\_field ‘photo’, ‘photo’ %\>

Ruby code to save in Database (postgresql):

def create  
@photo = Photo.new(params[:photo])  
if @photo.save  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

Now i would like to know where i should add your code

(mentioned above)

Thanks  
Priya
