You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Sinatra提供YAML文件服务而非触发下载?

Sinatra返回YAML文件内容而非触发下载

要实现仅返回YAML文件内容、不触发浏览器下载,有两种简单的解决方式:

  • 修改send_file参数:
    问题出在send_file默认会添加Content-Disposition: attachment响应头,导致浏览器触发下载。只需显式设置:disposition => 'inline',告诉浏览器直接渲染内容:

    get '/yaml/en' do
      send_file './en.yml', :type => 'text/yaml', :disposition => 'inline'
    end
    
  • 直接读取并返回文件内容:
    跳过send_file,直接读取文件内容作为响应体返回,同时设置正确的Content-Type:

    get '/yaml/en' do
      content_type 'text/yaml'
      File.read('./en.yml')
    end
    

两种方式都能让gem通过HTTP GET请求该端点时,直接获取到YAML文本内容,无需处理下载逻辑。

内容的提问来源于stack exchange,提问作者AlbertMunichMar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 06:54:06