【问题标题】:Mime content type for gpx not recognized with paperclip gem回形针宝石无法识别 gpx 的 Mime 内容类型
【发布时间】:2016-05-05 13:23:55
【问题描述】:

我在 rails 4.26 和 ruby​​ 2.3.1 中有一个应用程序,带有回形针 4.3.6、firefox 和 Ubuntu 14.0.4 64,我用它来上传带有代码的 gpx 文件

validates_attachment_content_type :gpx, :content_type => { content_type: 'application/xml'

也试过

validates_attachment_content_type :gpx, :content_type => { content_type: 'application/gpx+xml' }

在我的 Track 模型中,但没有通过有效 gpx 文件的验证。

如果我通过扩展名进行验证,例如

validates_attachment_file_name :gpx, matches: /gpx\Z/

加载正常。

当我运行文件命令时:

$ file demofilelite.gpx --mime-type -b

我明白了 应用程序/xml 作为输出

我尝试在 config/initializers 中使用 paperclip.rb 文件

Paperclip.options[:content_type_mappings] = { gpx: %w(application/xml) }

但它也没有工作。 如何让回形针通过 mime 内容识别 gpx 文件?

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    我设法让它在我的 track.rb 模型文件中使用此验证:

    validates :gpx, presence: true, attachment_content_type: { content_type: ["application/gpx+xml","text/xml","application/xml","application/octet-stream"] }
    

    这是 Edge、chrome 和 firefox 的不同标题。 并在 config/initializers/paperclip.rb 中创建一个初始化器:

    Paperclip.options[:content_type_mappings] = { :gpx => ["application/xml"] }
    

    处理文件命令响应。 这将加载 gpx 和 xml 文件。如果您只想要 gpx,则必须向您的模型添加另一个验证:

    validates_attachment_file_name :gpx, matches: /gpx\Z/
    

    【讨论】:

      【解决方案2】:

      你可以试着把这条线放在你的 confing/initilizer/mime_type.rb

      Mime::Type.register "application/gpx", :gpx

      【讨论】:

      • 这部分解决了反欺骗引擎的问题,但它不起作用,因为事实证明每个浏览器都会为 gpx 文件发送不同类型的 mime 头
      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 2023-03-17
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 2011-01-12
      相关资源
      最近更新 更多