# Buffered image to file in rails

**URL:** <https://www.ruby-forum.com/t/buffered-image-to-file-in-rails/136550>\
**Category:** Rails\
**Created:** [May 3, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/buffered-image-to-file-in-rails/136550 "2008-05-03T13:45:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratnavel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6f9a4e/32.png) [@ratnavel](https://www.ruby-forum.com/u/ratnavel)\
**Post date:** [May 3, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/buffered-image-to-file-in-rails/136550/1 "2008-05-03T13:45:07Z")

</div>

Hi,

I was trying to integrate a scanner with my application. After a long  
struggle i succeeded in integrating, Now i could see the captured image  
in my browser, But now i need to upload that image in buffer to the file  
system(Server Side) or to data base.

How to carry out this any suggestions?

Thanks,  
Ratnavel.

---

<div class="post-metadata">

**Author:** ![ratnavel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6f9a4e/32.png) [@ratnavel](https://www.ruby-forum.com/u/ratnavel)\
**Post date:** [May 3, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/buffered-image-to-file-in-rails/136550/2 "2008-05-03T15:06:35Z")

</div>

Ratnavel S. wrote:

> Hi,
> 
> I was trying to integrate a scanner with my application. After a long  
> struggle i succeeded in integrating, Now i could see the captured image  
> in my browser, But now i need to upload that image in buffer to the file  
> system(Server Side) or to data base.
> 
> How to carry out this any suggestions?
> 
> Thanks,  
> Ratnavel.

* * *

## Store Image in Database

~\> ruby script/generate model picture

* * *

## Migration

class CreatePicture \< ActiveRecord::Migration  
def self.up  
create\_table :global\_configs do |t|  
t.column :data, :blob  
t.column :content, :string  
end  
end

def self.down  
drop\_table :kamars  
end  
end

~\> rake db:migrate

* * *

## VIEW # for FORM to Upload

```
<% form_tag ({:action => "upload_pict" }, {:multipart => true}) do 

```

%\>  
\<%= file\_field “web”, “image” %\>  
\<%= submit\_tag “Upload” %\>  
\<% end %\>  
\<%= end\_form\_tag %\>

* * *

## CONTROLLER # store image into DB

class TeaPociController \< ApplicationController

…

def upload\_pict

if params[:web][‘image’].size \> 1  
@type\_file =  
File.extname(params[:web][‘image’].original\_filename.gsub(/[^\w.\_-]/,“”))

#Check Image, Image should be Picture  
if @type\_file == “.jpeg” || @tipe\_file == “.gif” || @tipe\_file == “.bmp”  
|| @tipe\_file == “.jpg” || @tipe\_file == “.png”

@picture = Picture.new

@picture.data = params[:web][‘image’].read  
@picture.content = params[:web][‘image’].content\_type.chomp

if @picture.save  
flash[:notice] = “Image is stored in database successfully.”  
end

else  
flash[:notice] = “File is not an image.”  
end

else  
flash[:notice2] = “Image is empty.”  
end

end

…

end

* * *

## CONTROLLER

class TeaPociController \< ApplicationController

…  
def display\_image  
@real\_picture = Picture.find(params[:id])  
send\_data(@real\_picture.data,  
:type =\> @real\_picture.content,  
:disposition =\> “inline”)  
end

end

* * *

## VIEW #DISPLAY IMAGE IN RHTML

GoodLuck,

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)
