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 content_type
is always "application/octet-stream"
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
相关文章
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
You can use
Mime::Type.lookup_by_extension(extention_name)
Thanks
You can use this reliable method base on the magic header of the file :
mimemagic gem will also do it
https://github.com/minad/mimemagic
from the oficial documentation
filemagic gem is good solution but depends lots of unnecessary gems. (rails, aws-sdk-core, ...)
If your app is small and only runs in Linux or OSX, consider to use
file
program:Note: Replace
__FILE__
with any expr contains the filepath.The ruby-filemagic gem will do it:
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.
In Ruby on Rails you can do: