Render PNG file in Rails

大城市里の小女人 提交于 2019-12-12 20:09:34

问题


I have a Rails app where I am trying to display a PNG file but I am getting this error:

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

I've added:

Mime::Type.register "image/png", :png

to the config/initializers/mime_types.rb

I'm referencing and rendering the png file in its controller with:

render :inline => @object.body.string, :content_type => @object.content_type || "img/png", :layout => false

EDIT :

This is the method in the controller

 def read_data
  resource = Aws::S3::Resource.new(client: @new_client)
  if @files.size == 0 && @links.size == 0 && resource.bucket(@bucket.name).objects.first != nil && !request.original_url.end_with?('/')
  if request.original_url.split('/')[-1] != @bucket.name && resource.bucket(@bucket.name).object(@prefix).exists?
     @object = @new_client.get_object(bucket: @bucket.name, key: @prefix)
     if @prefix.present? && @object.last_modified && (@object.content_type.match(/text/) || @object.content_type.match("application/json") || @prefix.end_with?('.json') || @prefix.end_with?('.html') || @prefix.end_with?('.txt') || @prefix.end_with?('.xml') || (@object.content_length > 0 && @object.content_type == ""))
      render :inline => @object.body.string, :content_type => @object.content_type || "text/plain", :layout => false
     elsif @prefix.end_with?('.png')
         send_data(@object.body, 
                   :type         => @object.content_type || "image/png",
                   :disposition  => 'inline')
     else
      redirect_to resource.bucket(@bucket.name).object(@prefix).presigned_url(:get, expires_in: 8) #presigned url expires after 8 ms
    end
  end
end

end


回答1:


Use the send_data method to send binary files:

send_data(@object.body.string, 
          type: @object.content_type || 'image/png', 
          disposition: 'inline')



回答2:


Use this code on your controller action:

respond_with @resource do |format|
    # format.html { send_data @resource.body } # => Download the image file.
    format.html { send_data @resource.body,
                  type: @resource.content_type || 'image/png',
                  disposition: 'inline' } # => Show in browser page.
end


来源:https://stackoverflow.com/questions/32568908/render-png-file-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!