Skip to content

Instantly share code, notes, and snippets.

@vysakh0
Forked from shrikanthkr/carrierwave-base64.md
Created April 8, 2014 06:56
Show Gist options
  • Save vysakh0/10098267 to your computer and use it in GitHub Desktop.
Save vysakh0/10098267 to your computer and use it in GitHub Desktop.

In the controller action

change_img_params(params[:background_image])

def change_img_params(img)
begin
  Base64.decode64(params[:user][img])
  if params[:user][img]
    params[:user][img] = file_decode(params[:user][img].split(',')[1],
                                          params[:user][:screen_name] )
  end
rescue Exception => e
  params[:user][img]
end

end

Helpers

def file_decode(base, filename)
    file = Tempfile.new([file_base_name(filename), file_extn_name(filename)])
    file.binmode
    file.write(Base64.decode64(base))
    file.close
    file
end

def file_base_name(file_name)
    File.basename(file_name, file_extn_name(file_name))
end

def file_extn_name(file_name)
    File.extname(file_name)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment