I'm trying to upload an image to PingFM. Their documentation says:
media – base64 encoded media data.
I can access this image via the URL. I tried (practically guessed) this:
ActiveSupport::Base64.encode64(open("http://image.com/img.jpg"))
But I get this error:
TypeError: can't convert Tempfile into String
from /usr/lib/ruby/1.8/base64.rb:97:in `pack'
from /usr/lib/ruby/1.8/base64.rb:97:in `encode64'
from (irb):19
from :0
In case it's useful to others, here's how to save a screenshot as base64 using
Watir
The beauty of this is you don't need to store the image itself
The
open
method:is returning a Tempfile object, while
encode64
expects a String.Calling
read
on the tempfile should do the trick:Encode a file to base64 encoding:
Decode base64 encoded file:
To encode a file:
To produce the file from the encoded string:
This will work too, it's a little cleaner
"How do you decode this back into a file?" - @user94154
Where
encoded_content
would be the previously encoded file content return value.Here's my solution:
1: Put this custom image_tag method into ApplicationHelper, and include ActiveSupport module
2: Then, inside the view you want to use base64 encoded image use the method like this:
3: DONE