Force browser to download file instead of opening it

后端 未结 1 1373
别那么骄傲
别那么骄傲 2020-12-09 06:40

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

相关标签:
1条回答
  • 2020-12-09 07:42

    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
    
    0 讨论(0)
提交回复
热议问题