Module XSendFile::Controller
In: lib/x_send_file/controller.rb

Methods

Public Instance methods

send_file_with_x_send_file(path, options = {})

Alias for x_send_file

Sends the file by setting the X-Sendfile HTTP header. You web server must be configured to respond to this header, or it will not work.

Options:

  • :filename - suggests a filename for the browser to use. Defaults to File.basename(path).
  • :type - specifies an HTTP content type. Defaults to ‘application/octet-stream’.
  • :disposition - specifies whether the file will be shown inline or downloaded. Valid values are ‘inline’ and ‘attachment’ (default).
  • :status - specifies the status code to send with the response. Defaults to ‘200 OK’.
  • :url_based_filename - set to true if you want the browser guess the filename from the URL, which is necessary for i18n filenames on certain browsers (setting :filename overrides this option).
  • :header - specifies the name to use for the X-Sendfile HTTP header Defaults to ‘X-Sendfile’.

Simple download:

  x_send_file '/path/to/file'

Show a JPEG in the browser:

  x_send_file('/path/to/image.jpg', :type => 'image/jpeg', :disposition => 'inline')

Send file using Lighttpd:

  x_send_file '/path/to/file, :header => 'X-LIGHTTPD-SEND-FILE'

x_send_file‘s options and defaults mirror those of send_file. Please see ActionController::Streaming#send_file for more detailed information about the options, HTTP specs, and possible security issues.

[Source]

    # File lib/x_send_file/controller.rb, line 52
52:     def x_send_file(path, options = {})
53:       raise ActionController::MissingFile, "Cannot read file #{path}" unless File.file?(path) and File.readable?(path)
54: 
55:       # pull in default values for options
56:       options.reverse_merge!(Plugin.options)
57:       options[:length]   ||= File.size(path)
58:       options[:filename] ||= File.basename(path) unless options[:url_base_filename]
59:   
60:       # set headers & send response
61:       send_file_headers! options
62:       response.headers[options[:header]] = path
63:       logger.info "Sending XSendFile header for #{path}" unless logger.nil?
64:       render options[:render]
65:     end

[Validate]