How to encode media in base64 given URL in Ruby

后端 未结 6 1108
野性不改
野性不改 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 16:05

    To encode a file:

    require 'base64'
    Base64.encode64(File.open("file_path", "rb").read)
    

    To produce the file from the encoded string:

    require 'base64'
    encoded_string = Base64.encode64(File.open("file_path", "rb").read)
    
    File.open(file_name_to_create, "wb") do |file|
        file.write(Base64.decode64(encoded_string))
    end
    

提交回复
热议问题