检测 Ruby 中上传文件的 MIME 类型 [英] Detect MIME type of uploaded file in Ruby
本文介绍了检测 Ruby 中上传文件的 MIME 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有防弹方法来检测 Ruby 或 Ruby on Rails 中上传文件的 MIME 类型?我正在使用 SWFupload 上传 JPEG 和 PNG,并且 content_type
始终是 "application/octet-stream"
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"
推荐答案
ruby-filemagic gem 可以做到:
require 'filemagic'
puts FileMagic.new(FileMagic::MAGIC_MIME).file(__FILE__)
# => text/x-ruby; charset=us-ascii
这个 gem 根本不看文件扩展名.它读取一些文件内容并使用它来猜测文件的类型.
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.
这篇关于检测 Ruby 中上传文件的 MIME 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文