小编Vin*_* Le的帖子

在Rails中渲染PNG文件

我有一个Rails应用程序,我试图显示一个PNG文件,但我收到此错误:

ActionView::Template::Error (Your template was not saved as valid UTF-8. Please either specify UTF-8 as the encoding for your template in your text editor, or mark the template with its encoding by inserting the following as the first line of the template:

# encoding: <name of correct encoding>.

The source of your template was:

?PNG

IHDR#??)    pHYs?]s? IDATx???g@SW?E??7?ZuV??Z?:j?m?m?Z??U[W?:??Z?*?j???@?3? I???p??}
????
???b?X?/???Z?I?N111,?O??x?T??x?mU????vtt
Run Code Online (Sandbox Code Playgroud)

我已经添加:

Mime::Type.register "image/png", :png
Run Code Online (Sandbox Code Playgroud)

到config/initializers/mime_types.rb

我在其控制器中引用并呈现png文件:

render :inline => @object.body.string, :content_type => @object.content_type || "img/png", :layout => false
Run Code Online (Sandbox Code Playgroud)

编辑: …

ruby png ruby-on-rails image-rendering

2
推荐指数
1
解决办法
3112
查看次数

标签 统计

image-rendering ×1

png ×1

ruby ×1

ruby-on-rails ×1