Vincent Vincent - 2 years ago 108
Ruby Question

Detect MIME type of uploaded file in Ruby

Is there a bullet proof way to detect MIME type of uploaded file in Ruby or Ruby on Rails? I'm uploading JPEGs and PNGs using SWFupload and

is always

Answer Source

The ruby-filemagic gem will do it:

require 'filemagic'

# => text/x-ruby; charset=us-ascii

This gem does not look at the file extension at all. It reads a bit of the file contents and uses that to guess the file's type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download