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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)