Method: Nitro::Render#send_file

Defined in:
lib/nitro/cgi/send_file.rb

#send_file(fname = nil, fullpath = false) ⇒ Object Also known as: sendfile

Send a file download to the client.

Like render and redirect, the action is exited upon calling

fname

That name of the file

path

Specifying true mean fname contains the full path.

The default, false, uses Server.public_root as the path.
return

true on success, false on failure

Examples

require ‘nitro/cgi/send_file’

class MyController < Nitro:Controller

def download(fname)
  send_file(fname)
end

end

class MyController < Nitro:Controller

def download()
  send_file('/etc/password', true)
end

end

Raises:



33
34
35
36
37
38
39
40
41
# File 'lib/nitro/cgi/send_file.rb', line 33

def send_file(fname=nil, fullpath=false)
  fname = fullpath ? fname : "#{Server.public_root}/#{fname}"
  f = File.open(fname, "rb")
  @context.response_headers["Cache-control"] = 'private'
  @context.response_headers["Content-Length"] = "#{File.size?(f) || 0}"
  @context.response_headers["Content-Type"] = 'application/force-download'
  @context.out = f
  raise RenderExit
end