本文介绍了ruby on rails:音频/mp3 内容标题下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.com/12345.mp3");

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

Seems like there should be an easy solution.

我确实找到了:

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

但我不确定读取文件和其他标题将如何/在何处发挥作用.

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

谢谢!

推荐答案

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

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

Rails 进程限制还有一些其他注意事项:

There are some other considerations with rails process limitations:

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

还有,send_filehttp://apidock.com/rails/ActionController/Streaming/send_file

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

这篇关于ruby on rails:音频/mp3 内容标题下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 04:53