Ruby on rails: audio / mp3 content header download

How do you set download headers in ruby ​​/ rails?

In php, I would set the title to download mp3 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.com/12345.mp3");

      

It looks like there should be a simple solution.

I found this:

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

      

But I'm not sure how / where the readfile and other headers will be read.

Thanks!

+2


a source to share


1 answer


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

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

      

There are several other considerations with rails process constraints:



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

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

+6


a source







All Articles