Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering file with MIME Type in rails

Here's the code:

render :file => @somedir + "/blah.xml"

...but the resulting MIME type is text/html when I check in FireBug. How do I specify a MIME type in this case?

like image 604
user38684 Avatar asked Nov 18 '08 20:11

user38684


3 Answers

Actually there are two ways to set the content-type (I think this is what you mean by mime-type). You should use the second option, if it works for your Rails version.

class FileController < ApplicationController

  def index
    filename = 'some.xml'

    extname = File.extname(filename)[1..-1]
    mime_type = Mime::Type.lookup_by_extension(extname)
    content_type = mime_type.to_s unless mime_type.nil?

    # 1
    #headers['Content-Type'] = content_type
    #render :file => filename

    # 2
    render :file => filename, :content_type => content_type
  end

end

Hope this helps!

like image 157
blindgaenger Avatar answered Oct 05 '22 22:10

blindgaenger


render :file => @somedir + "/blah.xml", :content_type => Mime::XML
like image 39
gerrit Avatar answered Oct 05 '22 23:10

gerrit


What about

headers["Content-Type"] = "text/xml"

? Hope that helps.

like image 42
Honza Avatar answered Oct 06 '22 00:10

Honza