ruby-on-rails-3 – Paperclip验证pdfs with content_type =’application / octet-stream’

我正在使用纸夹进行文件上传.验证如下:

validates_attachment_content_type:upload,:content_type => [‘application / pdf’],
:if => Proc.new {| module_file | !module_file.upload_file_name.blank? },
:message => “必须以”.pdf“格式”

但是,我的客户今天抱怨说他无法上传pdf.调查后,我从请求头知道,提交的文件是content_type = application / octet-stream.

允许应用程序/八位字节流将允许许多类型的文件进行上传.

请建议一个解决方案来处理这个.

解决方法

像回形针似乎没有正确检测到内容类型.以下是使用自定义内容类型检测和验证(代码在模型中)来修复它的方法
VALID_CONTENT_TYPES = ["application/zip","application/x-zip","application/x-zip-compressed","application/pdf","application/x-pdf"]

before_validation(:on => :create) do |file|
  if file.media_content_type == 'application/octet-stream'
    mime_type = MIME::Types.type_for(file.media_file_name)    
    file.media_content_type = mime_type.first.content_type if mime_type.first
  end
end

validate :attachment_content_type

def attachment_content_type
  errors.add(:media,"type is not allowed") unless VALID_CONTENT_TYPES.include?(self.media_content_type)
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...