Read more

Using mime types with send_file

Deleted user #4117
April 22, 2015Software engineer

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

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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
Posted to makandra dev (2015-04-22 09:43)