加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Paperclip验证pdfs with content_type =’ap

发布时间:2020-12-16 22:58:29 所属栏目:百科 来源:网络整理
导读:我正在使用纸夹进行文件上传.验证如下: validates_attachment_content_type:upload,:content_type = [‘application / pdf’], :if = Proc.new {| module_file | !module_file.upload_file_name.blank? }, :message = “必须以”.pdf“格式” 但是,我
我正在使用纸夹进行文件上传.验证如下:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读