[Rails] Image upload problems

Berndt Jung berndtj at gmail.com
Tue Jan 4 19:41:47 GMT 2005


So, like others on this list, I have started a picture store app.  I
have followed the directions in the "how to" section for file uploads,
as well as the previous posters here.  Basically, it seems as though
it should be working, but the images become corrupt.  I can view them,
but they look awful.  I tried resizing and formatting with RMagick
(really cool extention), but it complained about the files I was
sending it.  So now I'm pretty sure that either ruby/rails is
corrupting the files, or mysql.

Anyone having similar issues?

Thanks,

Berndt

MySql.dump

CREATE TABLE `photos` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` varchar(127) default NULL,
  `size` int(11) default NULL,
  `mime` varchar(127) default NULL,
  `picture` mediumblob NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `name` (`name`)
) TYPE=MyISAM

rails code:

def create
   #render_text @params['photo']['tmp_file'].local_path
   @photo = Photo.new do |p|
     p.name = @params['photo']['tmp_file'].original_filename.gsub(/[^a-zA-Z0-9.]/,
'_')
     p.size = @params['photo']['tmp_file'].size
     p.mime = @params['photo']['tmp_file'].content_type
     p.picture = @params['photo']['tmp_file'].read
  end
   @params.delete("photo" => "tmp_file")
   if @photo.save
      redirect_to :action => "show/" + @photo.id.to_s
   else
      render "photo/edit" 
   end
  end

def show
    #@image = Photo.find(@params['id'])
    #send_data @image.picture, :filename => @image.name, :type =>
@image.mime, :disposition => "inline"
    @image = Photo.find(@params["id"])
		@response.headers["Content-type"] = @image.mime
		render_text  @image.picture
  end


More information about the Rails mailing list