加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 强制浏览器下载文件而不是打开它

发布时间:2020-12-16 21:06:25 所属栏目:百科 来源:网络整理
导读:我想将 http://foobar.com/song.mp3作为song.mp3下载,而不是让Chrome在原生的 audio中打开它.播放器在浏览器中. 我怎么能这样做呢? 解决方法 您只需要确保发送这些标头: Content-Disposition: attachment; filename=song.mp3;Content-Type: application/oc
我想将 http://foobar.com/song.mp3作为song.mp3下载,而不是让Chrome在原生的< audio>中打开它.播放器在浏览器中.

我怎么能这样做呢?

解决方法

您只需要确保发送这些标头:
Content-Disposition: attachment; filename=song.mp3;
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

send_file方法为您完成:

get '/:file' do |file|
  file = File.join('/some/path',file)
  send_file(file,:disposition => 'attachment',:filename => File.basename(file))
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读