ActionDispatch::Http::UploadedFile to Base64

ruby, ruby-on-rails

Solution

Just solve it using this code:

  file = 
   target_params[:image].tempfile.open.read.force_encoding(Encoding::UTF_8)
  Base64.encode64(file)

Problem

I'm currently working on a ruby on rails project. In the project I have a form with a input file type (An image) and I need to convert the image to base64 (The project connects to an external api, so the image needs to be in base64) So far I have tried to do this `Base64.encode64(target_params[:image].read)` but I get an empty string as result.

Original source