Carlos Guerrero
Carlos Guerrero

Reputation: 372

ActionDispatch::Http::UploadedFile to Base64

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.

Upvotes: 6

Views: 2396

Answers (1)

Carlos Guerrero
Carlos Guerrero

Reputation: 372

Just solve it using this code:

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

Upvotes: 10

Related Questions