Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display base64 encoded image in rails

I'm sending a base64 image to my controller and I'm saving it as it is. Now I need to display that image. This is what I'm doing to display but image is not showing up:

<img src="<%= Base64.decode64(@data_obj.first.desc) %>"/>

In order to encode I'm using this java-script function encodeURIComponent();

My encoded image format:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/........
like image 551
Sachin Prasad Avatar asked Apr 23 '13 07:04

Sachin Prasad


Video Answer


2 Answers

You don't need to decode the base64

<img src="data:image/jpeg;base64,..." />

Should work

like image 157
Frederick Cheung Avatar answered Oct 09 '22 03:10

Frederick Cheung


Using Helper: <%= image_tag "data:image/jpeg;base64,#{@image}" %>
Do not forget to put double quotes, because the interpolation using #{}

like image 38
Luizinho Avatar answered Oct 09 '22 02:10

Luizinho