从控制器操作强制下载公共目录中的文件?

Rya*_*nce 1 ruby-on-rails header

我这样做了:

<% response.headers['Content-Disposition'] = "attachment; 
        filename=somefile.txt" -%>

I am a text file!
Run Code Online (Sandbox Code Playgroud)

我想在我的公共文件夹中强制下载文件而不显示路径,所以我有一个控制器而不是检查一些参数来知道我的文件的位置(在我的公共文件夹中)然后我想要强制下载:

<% response.headers['Content-Disposition'] = "attachment; 
        filename=#{@invoice.file_name}" %>

How do I get the file content to be here rather than this text?
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

Mik*_*bee 5

我认为send_file会做你想要的.

send_file '/path/to.file', :type => 'text/plain', :disposition => 'inline'
Run Code Online (Sandbox Code Playgroud)

  • 只需使用`, :disposition =&gt; 'attachment'` 来真正`强制` 下载。 (2认同)