How to encode media in base64 given URL in Ruby

后端 未结 6 1099
野性不改
野性不改 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:50

    The open method:

    open("http://image.com/img.jpg")
    

    is returning a Tempfile object, while encode64 expects a String.

    Calling read on the tempfile should do the trick:

    ActiveSupport::Base64.encode64(open("http://image.com/img.jpg") { |io| io.read })
    

提交回复
热议问题