Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
846 views
in Technique[技术] by (71.8m points)

mysql - Rails: displaying an image from a blob field in a database

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!

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...