在Ruby中检测上传文件的MIME类型 [英] Detect MIME type of uploaded file in Ruby
本文介绍了在Ruby中检测上传文件的MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
content_type
总是application / octet-stream
解决方案
ruby-filemagic 宝石将会这样做:
pre $ $ $ $ c $ require $'$ file $'
$ b $ FileMagic.new(FileMagic :: MAGIC_MIME) .file(__ FILE__)
#=>文本/ X-红宝石; charset = us-ascii
这个gem根本没有看文件扩展名。它读取一些文件内容,并用它来猜测文件的类型。
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"
解决方案
The ruby-filemagic gem will do it:
require 'filemagic'
puts FileMagic.new(FileMagic::MAGIC_MIME).file(__FILE__)
# => 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.
这篇关于在Ruby中检测上传文件的MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文