Sha256: 48026617684499f5cd73f9a0d2546c6c9e6823aa93cdcf4d2b9afbff1af785da

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

module Ramaze
  module Helper
    ##
    # 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.
    #
    module SendFile
      ##
      # The send_file method streams the specified file to the user's browser.
      #
      # @param [String] filename The name or path to the file which will be 
      #  streamed to the user.
      # @param [String] content_type 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'.
      # @param [String] content_disposition Value for the Content-Disposition 
      #  header.
      #
      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
    end # SendFile
  end # Helper
end # Ramaze

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ramaze-2023.01.06 lib/ramaze/helper/send_file.rb
ramaze-2012.12.08 lib/ramaze/helper/send_file.rb
ramaze-2012.12.08b lib/ramaze/helper/send_file.rb
ramaze-2012.04.14 lib/ramaze/helper/send_file.rb
ramaze-2012.03.07 lib/ramaze/helper/send_file.rb
ramaze-2011.12.28 lib/ramaze/helper/send_file.rb
ramaze-2011.10.23 lib/ramaze/helper/send_file.rb
ramaze-2011.07.25 lib/ramaze/helper/send_file.rb