Using mime types with send_file

When using send_file (for example for attachments of any kind), make sure your application knows the correct mime types so that all browsers can handle the files. It is much more convenient for users if they can decide to open a file directly instead of having to save it first.

For Rails >= 3.2

Simply put your mime types in config/initializers/mime_types.rb. send_file will take care of everything else.

For Rails < 3.2

Put your mime types in config/initializers/mime_types.rb. Additionally, tell send_file to use them (for example like this):

file = @attachment.file.path
file_extension = File.extname(file)[1..-1]
options = {}
options[:type] = Mime::Type.lookup_by_extension(file_extension).to_s
send_file file, options
About 9 years ago