【问题标题】:Force browser to download file instead of opening it强制浏览器下载文件而不是打开它
【发布时间】:2011-06-19 15:07:07
【问题描述】:

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

我怎样才能完成这个?

【问题讨论】:

    标签: ruby sinatra


    【解决方案1】:

    您只需要确保发送这些标头:

    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
    

    【讨论】:

    猜你喜欢
    • 2013-04-13
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 2020-05-04
    • 2013-10-25
    相关资源
    最近更新 更多