开发者

ruby on rails: audio/mp3 content header download

开发者 https://www.devze.com 2022-12-30 01:00 出处:网络
How do you set the headers for downloads in ruby/rails? In php I\'d set the header for an mp3 download like this:

How do you set the headers for downloads in ruby/rails?

In php I'd set the header for an mp3 download like this:

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

    @readfile("http://example.c开发者_开发知识库om/12345.mp3");

Seems like there should be an easy solution.

I did find this:

response.headers['Content-type'] = 'Content-type: audio/mp3'

But I'm not sure how/where the readfile would come into play and other headers.

Thanks!


Found the answer. send_file should be used in the controller.

  def download
       send_file "/path/to/file.mp3", :type=>"audio/mp3", :filename => "filenamehere.mp3"
  end

There are some other considerations with rails process limitations:

See here: http://www.therailsway.com/2009/2/22/file-downloads-done-right

Also, send_file http://apidock.com/rails/ActionController/Streaming/send_file

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号