# Get content\_type of uploaded file when using file-column

**URL:** <https://www.ruby-forum.com/t/get-content-type-of-uploaded-file-when-using-file-column/49146>\
**Category:** Rails\
**Created:** [November 30, 2005, 6:11pm UTC](https://www.ruby-forum.com/t/get-content-type-of-uploaded-file-when-using-file-column/49146 "2005-11-30T18:11:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [November 30, 2005, 6:11pm UTC](https://www.ruby-forum.com/t/get-content-type-of-uploaded-file-when-using-file-column/49146/1 "2005-11-30T18:11:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [November 30, 2005, 8:37pm UTC](https://www.ruby-forum.com/t/get-content-type-of-uploaded-file-when-using-file-column/49146/2 "2005-11-30T20:37:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [November 30, 2005, 10:32pm UTC](https://www.ruby-forum.com/t/get-content-type-of-uploaded-file-when-using-file-column/49146/3 "2005-11-30T22:32:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [December 6, 2005, 10:11am UTC](https://www.ruby-forum.com/t/get-content-type-of-uploaded-file-when-using-file-column/49146/4 "2005-12-06T10:11:14Z")

</div>

On 11/30/05, Mattias B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
