[Rails] Image upload problems
"Luis G. Gómez"
lgomez at vfxnetwork.com
Tue Jan 4 20:33:11 GMT 2005
I can't take a look at it now but have a look at what I have. It may
solve your probem. I don't get corrupted files. I use it for any type of
document not only images.
=====
CREATE TABLE `documents` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
`size` float NOT NULL default '0',
`last_mod` timestamp NOT NULL,
`mime` varchar(100) NOT NULL default '',
`document` blob NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM DEFAULT CHARSET=latin1;
=====
require 'abstract_application'
require 'document'
class DocumentController < AbstractApplicationController
def index
@page_title="Documents"
@documents=Document.find_all
end
def create
@document=Document.new do |d|
d.name=@params["document"].original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
d.size=@params["document"].size
d.mime=@params["document"].content_type
d.document=@params["document"].read
end
@params.delete("document")
@document.save
redirect_to :action=>"index"
end
def destroy
Document.find(@params["id"]).destroy
redirect_to :action=>"index"
end
def download
@document=Document.find(@params["id"])
@response.headers["Pragma"]=""
@response.headers["Cache-Control"]=""
@response.headers["Content-type"]=@document.mime
@response.headers["Content-Disposition"]="attachment;
filename=#{@document.name}"
@response.headers["Accept-Ranges"]="bytes"
@response.headers["Content-Length"]=@document.document.length
@response.headers["Content-Transfer-Encoding"]="binary"
@response.headers["Content-Description"]="File Transfer"
render_text @document.document
end
def render_image
@document=Document.find(@params["id"])
@response.headers["Content-type"]=@document.mime
render_text @document.document
end
end
=====
Berndt Jung wrote:
> 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
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
More information about the Rails
mailing list