Rendering file with MIME Type in rails

后端 未结 5 502
误落风尘
误落风尘 2020-12-28 17:52

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

相关标签:
5条回答
  • 2020-12-28 17:57

    Take a look here. Basically you need to use render :xml => blah.to_xml

    0 讨论(0)
  • 2020-12-28 18:02

    What about

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

    ? Hope that helps.

    0 讨论(0)
  • 2020-12-28 18:15

    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!

    0 讨论(0)
  • 2020-12-28 18:17

    Per http://api.rubyonrails.org/classes/Mime/Type.html, you could specify it like so:

    render file: @somedir + "/blah.xml", mime_type: Mime::Type.lookup("text/xml")  
    
    0 讨论(0)
  • 2020-12-28 18:19
    render :file => @somedir + "/blah.xml", :content_type => Mime::XML
    
    0 讨论(0)
提交回复
热议问题