Ruby-on-rails 在 rails 中使用 send_file
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2663018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using send_file in rails
提问by sgi
I'm sending a ms-word file using rails. i.e when I click on a link, a doc file from tmp folder (in project) is sent.
我正在使用 rails 发送一个 ms-word 文件。即,当我单击链接时,会发送来自 tmp 文件夹(在项目中)的 doc 文件。
The code I'm using is
我正在使用的代码是
@filename ="#{RAILS_ROOT}/tmp/test/test.doc"
send_file(@filename ,
:filename => "test",
:type => 'application/msword',
:disposition => 'attachment',
:streaming => 'true',
:buffer_size => '4096')
It's working, but it's sending an empty file. Content is missing in the file. Any suggestions?
它正在工作,但它正在发送一个空文件。文件中缺少内容。有什么建议?
回答by retro
There is no send_file:streaming option, it is :stream. You're passing bad parameters types. :buffer_size should be number, not a string. :stream should be boolean, not string.
没有send_file:streaming 选项,它是 :stream。您正在传递错误的参数类型。:buffer_size 应该是数字,而不是字符串。:stream 应该是布尔值,而不是字符串。
:stream => true,
:buffer_size => 4096,
You need only filename parameter (if you want to send file with another name than the original). Other options you are using are default (except :type).
您只需要 filename 参数(如果您想发送与原始名称不同的文件)。您使用的其他选项是默认的(除了 :type)。
Can you try this ?
你能试试这个吗?
@filename ="#{RAILS_ROOT}/tmp/test/test.doc"
send_file(@filename, :filename => "test.doc")
回答by seanbehan
comment out the following line in config/environments/production.rb
注释掉以下行 config/environments/production.rb
config.action_dispatch.x_sendfile_header = "X-Sendfile"
回答by Mukesh
In your view =>
<%= link_to "click here to download", signed_feeds_pdf_path(:feed_image_path => feed_image.feedimage.path), target: '_self' %>
In your controller =>
def pdf
file_name = params[:feed_image_path].split('/').last
@filename ="#{Rails.root}/public/uploads/feed_image/feedimage/#{file_name}"
send_file(@filename ,
:type => 'application/pdf/docx/html/htm/doc',
:disposition => 'attachment')
end
回答by simianarmy
Try sending with :disposition => 'inline'
尝试使用 :disposition => 'inline' 发送
send_file path, :type => 'application/msword', :disposition => 'inline'

