Render PNG file in Rails

时间:2015-09-14 15:51:14

标签: ruby-on-rails ruby png image-rendering

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

2 个答案:

答案 0 :(得分:8)

Use the send_data method to send binary files:

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

答案 1 :(得分:0)

在控制器操作上使用此代码:

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