Using mime types with send_file

Posted . Visible to the public.

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
Judith Roth
Last edit
Henning Koch
Keywords
content, type, ruby, on, rails, 2, 2.3, 2.3.18
License
Source code in this card is licensed under the MIT License.
Posted by Judith Roth to makandra dev (2015-04-22 07:43)