【问题标题】:Content type validations for fonts using paperclip使用回形针对字体进行内容类型验证
【发布时间】:2016-03-20 18:27:45
【问题描述】:

我正在尝试将字体文件作为附件上传到回形针中,但出现此错误。

ActiveRecord::RecordInvalid: Validation failed: File content type is invalid, File is invalid

这是我尝试过的

validates_attachment_content_type :file, content_type:['application/x-font-opentype','application/x-font-truetype','application/octet-stream']

还有我的模特

class OtherFont < ActiveRecord::Base
    has_many :texts
  has_attached_file :file, default_url: "/images/:style/missing.png"
   validates_attachment_content_type :file, content_type:['application/x-font-opentype','application/x-font-truetype','application/octet-stream']
  has_attached_file :file,
                    :storage => :s3,
                    :path => "fonts/:id/:style_:extension",
                    :s3_credentials => Proc.new{|a| a.instance.s3_credentials }
  def s3_credentials
    {:bucket => ENV['bucket'], :access_key_id => ENV['access_key_id'], :secret_access_key => ENV['secret_access_key']}
  end                  
end

【问题讨论】:

    标签: ruby-on-rails paperclip paperclip-validation


    【解决方案1】:

    .otf 和 .ttf 的 content_type 被检测为“application/octet-stream”

    您必须在 /initialize/paperclip.rb 中创建内容类型映射

    Paperclip.options[:content_type_mappings] = {
       otf: 'application/x-font-opentype',
       ttf: 'application/x-font-truetype'
    }
    

    对于 .woff,content_type 是 'application/font-woff'。 .woff 的内容类型被正确检测,不需要包含在 content_type_mappings 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      相关资源
      最近更新 更多