Method: Merb::ControllerMixin#send_file

Defined in:
lib/merb-core/controller/mixins/controller.rb

#send_file(file, opts = {}) ⇒ Object

Sends a file over HTTP. When given a path to a file, it will set the right headers so that the static file is served directly.

Parameters

file<String>

Path to file to send to the client.

opts<Hash>

Options for sending the file (see below).

Options (opts)

:disposition<String>

The disposition of the file send. Defaults to “attachment”.

:filename<String>

The name to use for the file. Defaults to the filename of file.

:type<String>

The content type.

Returns

IO

An I/O stream for the file.



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/merb-core/controller/mixins/controller.rb', line 152

def send_file(file, opts={})
  opts.update(Merb::Const::DEFAULT_SEND_FILE_OPTIONS.merge(opts))
  disposition = opts[:disposition].dup || 'attachment'
  disposition << %(; filename="#{opts[:filename] ? opts[:filename] : File.basename(file)}")
  headers.update(
    'Content-Type'              => opts[:type].strip,  # fixes a problem with extra '\r' with some browsers
    'Content-Disposition'       => disposition,
    'Content-Transfer-Encoding' => 'binary'
  )
  File.open(file, 'rb')
end