Forum: Ruby on Rails How to upload only jpeg & gif & png images into public/image

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Surekha M. (Guest)
on 2006-05-04 11:28
(Received via mailing list)
Hello iam new to rubyonrails. Please any one help me out in "How to
upload
only jpeg & gif & png images into public/images using rubyonrails"

Thanks in Advance
Enrico T. (Guest)
on 2006-05-04 11:40
(Received via mailing list)
hi there,
I hate when people send me an URL instead of an answer but I think you
should read this one: :-)
http://wiki.rubyonrails.com/rails/pages/HowtoUploadFiles
to check the file type the example talks about
person['picture'].content_type
I've never tried it BTW,
HTH!
Enrico

2006/5/4, Surekha M. <removed_email_address@domain.invalid>:
>
>


--
"The only thing necessary for the triumph of evil
is for good men to do nothing"
 Edmund Burke
Surekha M. (Guest)
on 2006-05-04 12:04
(Received via mailing list)
Already I go thru this link, I implemented in my application. The images
are
uploaded but those are not as the original images. Overlapped with some
colours. But one image is uploaded successfully its size is 977Bytes.
If it is 2, or 1.4 KB or more than this not uploaded as original images

so plz help me out in this thing
Jon M. (Guest)
on 2006-05-11 01:53
Surekha M. wrote:
> Already I go thru this link, I implemented in my application. The images
> are
> uploaded but those are not as the original images. Overlapped with some
> colours. But one image is uploaded successfully its size is 977Bytes.
> If it is 2, or 1.4 KB or more than this not uploaded as original images
>
> so plz help me out in this thing

Make sure that in your database table you have the data column set to
'longblob' not just blob.  it will crop the image if you don't!

jon
Dorian (Guest)
on 2006-05-12 16:47
Well,

     It looks like no one intends to help you, which is pretty much the
experience that Ive had on this mailing list.  So heres some of my code.
This is in my pcomic_admin controller that allows editors for my
magazine to submit comics.  The code is not finished yet.  It doesnt
check for errors in alot of places that it should.  It also does not
check the file size.  Im also wary of allowing uploads to the public
directory.  Im thinking that the image should actually go into a sub
folder with black hole permissions, similar to an ftp setup.  Maybe
others on this list can give an opinion, that would be nice for once.

Dorian


  def create
    if @session['user'].nil?
	redirect_to :action => 'login'
    elsif params[:comicpic].content_type.chomp == "image/jpeg"
	@user = User.find(@session['user'].id)
	File.open("../public/comics/#{@user.id}/#{params[:comicpic].original_filename}",
"w") do
		|f| f.write(params[:comicpic].read)
	end
    @comic = Pendingcomic.new(params[:comic])
	@comic['image_url'] =
"/comics/#{@user.id}/#{params[:comicpic].original_filename}"
	@user.pendingcomics.push(@comic)
      	redirect_to :action => 'list'
    else
      	redirect_to :action => 'list'
    end
  end



Surekha M. wrote:
> Hello iam new to rubyonrails. Please any one help me out in "How to
> upload
> only jpeg & gif & png images into public/images using rubyonrails"
>
> Thanks in Advance
Szymon N. (Guest)
on 2006-05-12 17:15
You could also use file_column plugin:
http://www.kanthak.net/opensource/file_column/

You can install the latest revision like this:
script/plugin install
http://opensvn.csie.org/rails_file_column/plugins/...

Then if you want you can generate docs for this plugin:
rake doc:plugins

Now if you want to allow uploading of specific formats you can do:

class Entry < ActiveRecord::Base
  file_column :image
  validates_file_format_of :field, :in => ["gif", "png", "jpg"]
end

And that's all. Really simple.
This topic is locked and can not be replied to.