Module: Ramaze::Helper::SendFile

Defined in:
lib/ramaze/helper/send_file.rb

Overview

The SendFile module can be used to stream a file to the user's computer. While the performance of the module isn't optimal it's convenient and relatively easy to use.

Instance Method Summary (collapse)

Instance Method Details

- (Object) send_file(filename, content_type = nil, content_disposition = nil)

The send_file method streams the specified file to the user's browser.

Parameters:

  • filename (String)

    The name or path to the file which will be streamed to the user.

  • content_type (String) (defaults to: nil)

    The type of file we're dealing with. For example, if we want to stream a JPG image we'd set this variable to 'image/jpg'.

  • content_disposition (String) (defaults to: nil)

    Value for the Content-Disposition header.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ramaze/helper/send_file.rb', line 20

def send_file(filename, content_type = nil, content_disposition = nil)
  content_type ||= Rack::Mime.mime_type(::File.extname(filename))
  content_disposition ||= File.basename(filename)

  response.body = ::File.open(filename, 'rb')
  response['Content-Length'] = ::File.size(filename).to_s
  response['Content-Type'] = content_type
  response['Content-Disposition'] = content_disposition
  response.status = 200

  throw(:respond, response)
end