Shrolox
Shrolox

Reputation: 663

Set Content-Type with send_file

I need to send a file with a specific content type (application/pkcs7-mime) for Apple universal links. I use the send_file function from rails, and add :type as a parameter, but the type is always text/plain.

Here is my controller:

class AppleController < ApplicationController
  def download_app_site_association
    send_file "#{Rails.root}/public/apple-app-site-association", {:filename => "apple-app-site-association", :type => "application/pkcs7-mime" , :x_sendfile => true}
  end
end

I also tried to add it like this:

send_file "#{Rails.root}/public/apple-app-site-association", :filename => "apple-app-site-association", :type => "application/pkcs7-mime" , :x_sendfile => true

and setting it manually like this:

response.headers["Content-type"] = "application/pkcs7-mime"
send_file "#{Rails.root}/public/apple-app-site-association", :filename => "apple-app-site-association", :x_sendfile => true

Can anyone explain me why this doesn't work ?

Upvotes: 3

Views: 2282

Answers (2)

Shrolox
Shrolox

Reputation: 663

I found the solution. The problem was that when i tried to get mywebsite.com/apple-app-site-association, the router was searching first for files in #{Rails.root}/public/ and then searching for routes in routes.rb

As my file and my route had the same name, the apple_controller was never reached.

So I just changed the file name.

Upvotes: 1

devanand
devanand

Reputation: 5290

There is a parameter you can set

http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_file

UPDATE: is the Mime type registered under config/initializers/mime_types.rb?

Upvotes: 0

Related Questions