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
精彩评论