Coding is an Art

Anoiaque::Ergasterium

18/07/2010

How to get and send image with Rails without paperclip or other image gem

How to get an image or binary data from an input file from client side, without paperclip or other image gem.
class Picture < ActiveRecord::Base 
  
  def image_file=(uploaded_data)
    self.content_type = uploaded_data.content_type
    self.filename = uploaded_data.original_filename
    self.image_data = uploaded_data.read
    self.size  = uploaded_data.size 
  end
  
end

In controller, to get the file data and to send it to client :

class PicturesController < ApplicationController
  
  def update
    @picture.image_file = params[:picture] #FileTemp object
    @picture.save
  end
  
  def code_image
    @image_data = Picture.find(params[:id])           
    @image = @image_data.binary_data           
    send_data (@image, :type => @image_data.content_type, :filename => @image_data.filename, :disposition => 'inline') 
  end

end

Aucun commentaire:

Enregistrer un commentaire

Fools ignore complexity. Pragmatists suffer it. Some can avoid it. Geniuses remove it.
Alan Perlis


The best way to predict the future is to invent it.
Alan Kay


Ne découvre de nouvelles terres que celui qui sait quitter tout rivage.
André gide


Qui ne doute pas acquiert peu
Léonard de Vinci


Our life is frittered away by detail... simplify, simplify.
Henry David Thoreau