6

I am developing a server backend for my iphone application. I need to upload a picture from my Iphone app to the rails server

I have a base64 encoded image in the post request from my app which I need to parse and send to the Paperclip plugin for the re-sizing of the image.So can anybody suggest me how to do it.

Help me!!!. Thanks in advance.

saket
  • 114
  • 1
  • 2
  • 7

2 Answers2

10

In order to save image with correct extension you have to specify content type. It's quite nice to have this in your model as method which is called before_validation

   StringIO.open(Base64.decode64(self.photo_base64)) do |data|
      data.original_filename = "image_name.jpg"
      data.content_type = "image/jpeg"
      self.photo = data
    end
inquisitive
  • 3,738
  • 6
  • 30
  • 56
Tombart
  • 30,520
  • 16
  • 123
  • 136
  • You should clarify that photo_base64 is not the the URI but rather the '' value within the URI string. Therefore, you need to discard the 'MIME-type' and 'encoding' if it does include these (ie., `photo_base64 .split(',').pop`) – user1322092 Jan 18 '14 at 16:53
3

Try this:

sio = StringIO.new(Base64.decode64(string))

[ source: base64 photo and paperclip -Rails ]

Community
  • 1
  • 1
amaseuk
  • 2,147
  • 4
  • 24
  • 43