0

I have the binary data for an image in my controller action. I need to display the image in the view. How can I do this? I'm using Ruby 1.9.2 and Rails 3.0.1.

3
  • You probably want to avoid outputting binary image data inline in an HTML view, opting instead for a separate request to a binary image resource (in which case you can output the data directly from your controller if there's no way you can write it to disk/external-storage where it belongs). Commented Jun 16, 2011 at 4:26
  • 1
    Check this - railsforum.com/viewtopic.php?id=4642 Commented Jun 16, 2011 at 4:37
  • Might checkout the paperclip gem/plugin Commented Jun 16, 2011 at 5:23

1 Answer 1

2

As coreyward above notes, it's not ideal to be doing this at all. But if you don't have an alternative (really, you probably do), you should look into data URIs.

It depends on what format your binary data is in, but the short version is that you will convert the data to a Base64-encoded string and then build a special URI that starts with data:, followed by the appropriate MIME type, and then the base64 string, and use that as the src attribute in a normal <img /> tag.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.