ruby-on-rails - 如何通过 send_file 设置内容长度

标签 ruby-on-rails sendfile

我不知道如何通过 send_file 设置内容长度。

我检查了 api,没有内容长度参数。

最佳答案

您可以为响应设置 header ,例如:

def download
  @file = Attachment.find params[:id]

  response.headers['Content-Length'] = @file.size.to_s
  send_file @file.path, :x_sendfile => true
end

您可以在 official docs 上找到有关响应对象的更多信息。

P.S: Header 需要是一个字符串才能在某些网络服务器上正常工作。

关于ruby-on-rails - 如何通过 send_file 设置内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29485042/

相关文章:

ruby-on-rails - Rails 3和fields_for与activemodel(无表)对象

linux - linux write 和 sendfile 系统调用的区别

ruby-on-rails - 使用 ActiveRecord 助手删除 VS 查找孤儿

ruby-on-rails - `Time.now.utc` Rails 和 `now() at time zone ' utc'` postgres 之间的区别

ruby-on-rails - Rspec 2 'should_receive' 不适用于 Mongoid::Document 类

javascript - 启动 Angular 服务器时出错

javascript - 快速发送文件中断

JavaScript:res.sendFile 不是函数

ruby-on-rails - Wicked_PDF 仅下载 - Rails 3.1

ruby-on-rails - 选择元素,simple_form 助手