强制浏览器下载文件而不是打开它 [英] Force browser to download file instead of opening it

查看:65
本文介绍了强制浏览器下载文件而不是打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载 http://foobar.com/song.mp3 作为 Song.mp3,而不是让 Chrome 在浏览器中的原生 播放器中打开它.

I would like to download http://foobar.com/song.mp3 as song.mp3, instead of having Chrome open it in its native <audio> player in the browser.

我怎样才能做到这一点?

How can I accmplish this?

推荐答案

你只需要确保发送这些标题:

You just need to make sure to send these headers:

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

这篇关于强制浏览器下载文件而不是打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆