【问题标题】:How to set a content-type for a specific file with Rack?如何使用 Rack 为特定文件设置内容类型?
【发布时间】:2011-03-19 14:15:43
【问题描述】:

我想让 Rack 提供具有特定内容类型的特定文件。这是一个 .htc 文件,它需要作为 text/x-component 提供,以便 IE 能够识别它。在apache中我会这样做

AddType text/x-component .htc

如何使用 Rack 实现这一点?目前该文件由 Rack::Static 提供,但我没有找到设置内容类型的选项。

【问题讨论】:

    标签: ruby internet-explorer rack file-type html-components


    【解决方案1】:

    或者只是为了回答问题,在config/initializers/mime_types.rb添加这个:

    Mime::Type.register "text/x-component", :htc
    

    【讨论】:

      【解决方案2】:

      您可以像这样更新您的config/initializers/mime_types.rb

      # Be sure to restart your server when you modify this file.
      
      # Add new mime types for use in respond_to blocks:
      # Mime::Type.register "text/richtext", :rtf
      # Mime::Type.register_alias "text/html", :iphone
      
      Rack::Mime::MIME_TYPES.merge!({
        ".ogg"     => "application/ogg",
        ".ogx"     => "application/ogg",
        ".ogv"     => "video/ogg",
        ".oga"     => "audio/ogg",
        ".mp4"     => "video/mp4",
        ".m4v"     => "video/mp4",
        ".mp3"     => "audio/mpeg",
        ".m4a"     => "audio/mpeg",
        ".htc"     => "text/x-component"
      })
      

      【讨论】:

      • 成功了,谢谢!我没有mime_types.rb,所以直接放到config.ru文件中。
      • 抱歉,你肯定没有,它来自 rails,但由于 rails 是一个机架,它可以工作。
      • 通常我的 Rack 项目没有 config/initializers 目录,只有我的 rails 项目有...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 2013-01-30
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      相关资源
      最近更新 更多