如何用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
相关产品推荐
相关产品推荐

