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.
-
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).coreyward– coreyward2011-06-16 04:26:30 +00:00Commented Jun 16, 2011 at 4:26
-
1Check this - railsforum.com/viewtopic.php?id=4642Dhruva Sagar– Dhruva Sagar2011-06-16 04:37:39 +00:00Commented Jun 16, 2011 at 4:37
-
Might checkout the paperclip gem/pluginMsencenb– Msencenb2011-06-16 05:23:20 +00:00Commented Jun 16, 2011 at 5:23
Add a comment
|
1 Answer
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.