Rails:显示数据库中blob字段的图像

时间:2022-01-06 00:23:53

So, I managed to get an image blob into my MySQL database (there's a big hex number in the field), but I can't find any documentation on how to display the image in a rails environment... when it prints out, it starts with a GIF89... and then the gobbledygook characters you see in a GIF when you open it in Notepad. : P Any clues would be greatly appreciated!

所以,我设法将一个图像blob放到我的MySQL数据库中(该字段中有一个大的十六进制数字),但我找不到任何关于如何在rails环境中显示图像的文档...当它打印出来时,它从一个GIF89开始...然后当你在记事本中打开它时,你在GIF中看到的gobbledygook字符。 :P任何线索将不胜感激!

Thanks.

3 个解决方案

#1


21  

The following code should work. In your controller, create a method:

以下代码应该有效。在您的控制器中,创建一个方法:


def show_image
    @user = User.find(params[:id])
    send_data @user.image, :type => 'image/png',:disposition => 'inline'
end

In your view:

在你看来:


<%= image_tag url_for(:controller => "mycontroller", :action => "show_image", :id => @user.id) %>

I would recommend using the Paperclip gem. It makes saving/viewing of images really easy.

我建议使用Paperclip gem。它使得保存/查看图像变得非常容易。

#2


11  

Because you mentioned "quick and dirty", I'll throw out this as an alternative-

因为你提到“快速而肮脏”,我会把它作为替代品 -

<%= ('<img src="data:image/jpg;base64,%s">' % Base64.encode64(@the_data)).html_safe %>

I think this is closest to what you wanted to do. There's a few reasons that this code shouldn't be used as-is, but it's simple. I'd have to think more about how bad of an idea is it is to mark the whole thing as html_safe. Also, this wouldn't work in older versions of IE.

我认为这最接近你想做的事情。这个代码不应该原样使用有几个原因,但它很简单。我必须更多地思考一个想法是多么糟糕,将整个事物标记为html_safe。此外,这在旧版本的IE中不起作用。

#3


5  

You should also add

你还应该添加

resources :users do
     get 'show_image', :on => :collection
 end

or

get users/show_image" => "users#show_image"

before

resources :users in route.rb file

resources:route.rb文件中的用户

#1


21  

The following code should work. In your controller, create a method:

以下代码应该有效。在您的控制器中,创建一个方法:


def show_image
    @user = User.find(params[:id])
    send_data @user.image, :type => 'image/png',:disposition => 'inline'
end

In your view:

在你看来:


<%= image_tag url_for(:controller => "mycontroller", :action => "show_image", :id => @user.id) %>

I would recommend using the Paperclip gem. It makes saving/viewing of images really easy.

我建议使用Paperclip gem。它使得保存/查看图像变得非常容易。

#2


11  

Because you mentioned "quick and dirty", I'll throw out this as an alternative-

因为你提到“快速而肮脏”,我会把它作为替代品 -

<%= ('<img src="data:image/jpg;base64,%s">' % Base64.encode64(@the_data)).html_safe %>

I think this is closest to what you wanted to do. There's a few reasons that this code shouldn't be used as-is, but it's simple. I'd have to think more about how bad of an idea is it is to mark the whole thing as html_safe. Also, this wouldn't work in older versions of IE.

我认为这最接近你想做的事情。这个代码不应该原样使用有几个原因,但它很简单。我必须更多地思考一个想法是多么糟糕,将整个事物标记为html_safe。此外,这在旧版本的IE中不起作用。

#3


5  

You should also add

你还应该添加

resources :users do
     get 'show_image', :on => :collection
 end

or

get users/show_image" => "users#show_image"

before

resources :users in route.rb file

resources:route.rb文件中的用户