5

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?

Phrogz
  • 296,393
  • 112
  • 651
  • 745
Mark
  • 39,169
  • 11
  • 42
  • 48

1 Answers1

13

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

The send_file method does it for you:

get '/:file' do |file|
  file = File.join('/some/path', file)
  send_file(file, :disposition => 'attachment', :filename => File.basename(file))
end
dontangg
  • 4,729
  • 1
  • 26
  • 38
  • +1 Beat me to it and a much better explanation than I was writing up. –  Feb 02 '11 at 00:08
  • Here are the [docs for `send_file`](http://sinatra.rubyforge.org/api/classes/Sinatra/Streaming.html#M000022). – Phrogz Feb 02 '11 at 20:42