How to encode media in base64 given URL in Ruby

后端 未结 6 1109
野性不改
野性不改 2020-12-04 15:08

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.

6条回答
  •  甜味超标
    2020-12-04 15:58

    This will work too, it's a little cleaner

     require 'base64'
    
     Base64.encode64(open("file_path").to_a.join)
    

    "How do you decode this back into a file?" - @user94154

     require 'base64'
    
     open('output_file_name.txt', 'w') do |f| 
       f << Base64.decode64( encoded_content )
     end
    

    Where encoded_content would be the previously encoded file content return value.

提交回复
热议问题