Forum: Ruby on Rails Get content_type of uploaded file when using file-column

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.
4d5e28840f8628550a22c410cb73ad60?d=identicon&s=25 Mattias Bud (mattias)
on 2005-11-30 18:11
I use the file-column plugin to handle a file upload module for a rails
app. Plain and simple:

class Media < ActiveRecord::Base
  file_column :file

What I now would like is to get the contetn_type or the IOString object
of the original uploaded file. file_column converts this to a string
object containing the path to the file.

Any idears?
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 kyle (Guest)
on 2005-11-30 20:37
(Received via mailing list)
You can't determine that from the extension of the uploaded file?
What exactly are you trying to do?

You can try to override the file= method

alias_method :old_file=, :file=
def file=(original_io)
  #do something with original io
  old_file=(original_io)
end
4d5e28840f8628550a22c410cb73ad60?d=identicon&s=25 Mattias Bud (mattias)
on 2005-11-30 22:32
kyle wrote:
> You can't determine that from the extension of the uploaded file?
> What exactly are you trying to do?
>
> You can try to override the file= method
>
> alias_method :old_file=, :file=
> def file=(original_io)
>   #do something with original io
>   old_file=(original_io)
> end

I can't make this work. File-column trabnsform the IO object to a String
object (the files location) This happens before I can get my hands on
the IOString object.

I could moove this to the controller but that feels like the wrong place
for this.
E6dcb4743b83392a4fc39693f110cc26?d=identicon&s=25 eugenol (Guest)
on 2005-12-06 10:11
(Received via mailing list)
On 11/30/05, Mattias Bodlund <mattias@oncotype.dk> wrote:
> >   old_file=(original_io)
> > end
>
> I can't make this work. File-column trabnsform the IO object to a String
> object (the files location) This happens before I can get my hands on
> the IOString object.
>
> I could moove this to the controller but that feels like the wrong place
> for this.



I feel the same but now I couldn't get the mime type from the model
declaration (ideally in a before_create hook). Then I wrote in some
controller:
def save
  media = Media.new(params[:media])
  # ...
  media.content_type  = params[:media][:file].content_type.strip
  media.save
end
This topic is locked and can not be replied to.