在Rails中使用MIME类型呈现文件 [英] Rendering file with MIME Type in rails
本文介绍了在Rails中使用MIME类型呈现文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码如下:
render :file => @somedir + "/blah.xml"
...但是当我检入FireBug时,得到的MIME类型是text/html.在这种情况下,如何指定MIME类型?
...but the resulting MIME type is text/html when I check in FireBug. How do I specify a MIME type in this case?
推荐答案
实际上,有两种方法可以设置content-type(我认为这就是mime-type的意思).如果适用于您的Rails版本,则应使用第二个选项.
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
希望这会有所帮助!
这篇关于在Rails中使用MIME类型呈现文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文