Save image uploaded from mobile devices to server(base64)

Yesterday there was a requirement where user wanted to uploaded his/her profile pic from mobile application.

There are two basic ways of doing that -

  1. Image will be directly uploaded to s3 bucket on aws and url will be provided to the server.
  2. base64 string of that image will be provided to the server.

Base64 - represents binary data in an ASCII string format by translating it into radix-64 representation. Each base64 digit represents 6 bits of data.

Base64 module in ruby provides encoding and decoding of binary data using a Base64 representation.

Here mobile device provides an image as base64 string to server and then server decodes that string back to form the same image.

  base64_string = Base64.encode64(File.join(Rails.root, 'public', 'hello.png'))
# -> "

Then manipulation of this base64 string on server would be like -

_profile_data = base64_string.match(/\Adata:([-\w]+\/[-\w\+\.]+)?;base64,(.*)/) || []
_profile_image_data = _profile_data[2]
extension = MIME::Types[_profile_data[1]].first.preferred_extension

tempfile = File.join(Rails.root, 'tmp', "#{ SecureRandom.uuid}.#{extension}"), "wb") do |file|
self.profile_pic =
File.delete(tempfile) if File.exists? tempfile
comments powered by Disqus