ruby on rails:音频/ mp3内容标题下载

djb*_*ick 2 ruby ruby-on-rails

如何在ruby/rails中设置下载标题?

在php中,我为这样的mp3下载设置了标题:

    header("Content-Transfer-Encoding: binary");
    header("Content-type: audio/mp3");
    header("Content-Disposition: attachment; filename=\"$songname.mp3\"");
    header("Content-Length: " . $size);

    @readfile("http://example.com/12345.mp3");
Run Code Online (Sandbox Code Playgroud)

似乎应该有一个简单的解决方案.

我确实发现了这个:

response.headers['Content-type'] = 'Content-type: audio/mp3'
Run Code Online (Sandbox Code Playgroud)

但我不确定readfile将如何/在哪里发挥作用和其他标题.

谢谢!

djb*_*ick 6

找到了答案.send_file应该在控制器中使用.

  def download
       send_file "/path/to/file.mp3", :type=>"audio/mp3", :filename => "filenamehere.mp3"
  end
Run Code Online (Sandbox Code Playgroud)

rails过程限制还有一些其他注意事项:

见这里:http: //www.therailsway.com/2009/2/22/file-downloads-done-right

另外,send_file http://apidock.com/rails/ActionController/Streaming/send_file