What is the best way to upload an image from a client to a Rails backend using Carrierwave. Right now our iOS developer is sending in the files as base64, so the requests co
Old question but I had to do a similar thing, upload image from base64 string which was passed on via a json request. This is what I ended up doing:
#some_controller.rb
def upload_image
set_resource
image = get_resource.decode_base64_image params[:image_string]
begin
if image && get_resource.update(avatar: image)
render json: get_resource
else
render json: {success: false, message: "Failed to upload image. Please try after some time."}
end
ensure
image.close
image.unlink
end
end
#some_model.rb
def decode_base64_image(encoded_file)
decoded_file = Base64.decode64(encoded_file)
file = Tempfile.new(['image','.jpg'])
file.binmode
file.write decoded_file
return file
end