Skilldrick
Skilldrick

Reputation: 70819

How do I render a base64-encoded image in Rails?

I have a base64-encoded JPG in my app, and I'd like to render that image as a JPG in a controller.

Upvotes: 5

Views: 4731

Answers (2)

Jerome Dalbert
Jerome Dalbert

Reputation: 10405

Here's how to do it with the usual render syntax:

render text: Base64.decode64(your_base_64_string),
       content_type: 'image/jpeg'

Upvotes: 4

Skilldrick
Skilldrick

Reputation: 70819

I worked this out as soon as I asked the question. Assuming you have the base64 string in @image_data:

send_data Base64.decode64(@image_data),
  :type => 'image/jpeg', :disposition => 'inline'

Upvotes: 7

Related Questions