Sha256: 36121b5b367cffbd0c26b0d5e925770eab6999855f26d9b2e170c5bf407ade65

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

require 'nitro/render'

module Nitro

module Render

  # 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
  
  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
  alias sendfile send_file

end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.41.0 lib/nitro/cgi/send_file.rb
nitro-0.40.0 lib/nitro/cgi/send_file.rb