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

问题描述:

我想下载http://foobar.com/song.mp3作为song.mp3,而不是让Chrome在其浏览器中的本机<audio>播放器中打开它。强制浏览器下载文件而不是打开它

我怎么能accmplish呢?

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

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 
+0

+1打我给它和一个更好的解释比我写作。 – 2011-02-02 00:08:44