4

Here my question, I'm using Rails 4 and paperclip to handle image uploading to my amazon s3 server. However, I want to allow user to use their own webcam to take a picture instead of selecting an existing file on their hard drive.

When I'm capturing the image, I get as a result an image data URI similar to that :

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAAFACAYAAADNkKWqAAAgAElEQVR4nDS7RXNj6bpuW/98xzkRZ8Paa62irMx0ZpptMTOzNMXMzAyWzGnNOW5DdRsOufFJzREPvb/wmkN6KiM9lhCf60gvbaTnJryUkF6LSE8FpLc00nME8SmF+FSEfQppHUF8LMA6i7SqIh36SA8FpHUUHlJIuwLsKkjzBGwCiNsAx6kDFkbEhQFx7EOcCEjrCNIsiDgKwFMZHrJIyzSsikjTKtJ8COs+H/M40tyEtAud3kwrSJsmx2EEaRFCnMVP31vGYGFHWl0ibe6Q9mHEcRpmRaRVDWkRg53A+yzI28ADDwXEWRhxZkRa+GGd5ufQizQLcpwKsC3DqoA0zSKN84jDLNI4wUfPiTR0w9yGNLYhTSOIkwofozzHeYqnoYOPmQ9pKiCOUjAt8d7OIc7CvPQdLMsqNjU1bwMnT10Pm6oPcZTivelkU5bx3FLwWLrlqXzJR+UcsSXnpani0FTzWFXy3DBxqMV46wq8tG3s8gr2RTPHZpxjNcax7ua9YuNQ0PNaM/He9PJUj/DaSPNU8/NStvNY9vBWcfNYMPBU1vBaM/NQMPFYsrNKaThUbOyqHhYFE5OMiklKyaZgZJYysipa2Nf8TJMeNsUA04KCSfYzg8QnVoVbVtkfTEP/YJv+k33uC5vUX8yFL+wKd4ziX5mk/6If/0I/fsM8b2AoKBnGTPQSDkYpFwPBwDStYVvVMcpc0o1c0BcUdGMaBnENVf8Z7fAZy6yKmaBkmpCzTt2zTHxmI1yyS2jZJdy85EJMo1YGUQ0TQcs0amKT9NKLKOgJl7SFc+rCJd2Mi27CRyfpo532U015KCbstNIWWuFresIdPUFLP+6gGbij7PxEP2qi4bAwCUfphRN0Qn46gp5+7IZe/IZeVMks4mPk9TCLuGmH9HSiZto+AwOfm14wRjcUoRH0UA2aybvkDDJmujEDNa+MbkhDx6+kFTDRibioh0yUg3JqYT15j4aqz0TeJSPvPSfv/UoroqQZ0lEPKmlELsn5/pNq4P/QCf0/Wt7f6Xr0dPxmWl4tBfsnMo7fyHjOyDn/Im35nWbknqxXTsFnpBt3UHHe0/Bekrf/k4LzK82AjIr7jGbgG1WfhozbRtLnI2EzUvbekzf9QcH8nZrzmpb7hpL7G0nHn+ScZxRt38kZP1F3fqfsvKDkuCduOCdrOads+0rJ8hcF8xkVxzVp0xkx3Tfylu8UjP9FVv8f5B1XJIwKyk4tWccVUf2/iBn+Tcz0mW7MTMZ8Rkr1n5RU/6BqOCetPiNm+I2g6v8S1/0XKeOvFIx/Ebv8D/KG/yVn/hdJ9T

My question is : how do I upload that image based on that data URI using paperclip in my image controller ?

Is there a way that I can pass that DATA URI directly to my controller and paperclip will take care of it? Or do I have to create and write a temp file to reconstruct the image and then pass it to paperclip?

Thanks a lot!!

1 Answer 1

4

Paperclip#io_adapters.for will try to find the right adapter for the job, in this case Paperclip::DataUriAdapter . You could also use Paperclip::DataUriAdapter directly of course.

def save_data_uri_to_attachment(data_uri)    

  # Instantiates Paperclip::DataUriAdapter attachment
  file = Paperclip.io_adapters.for(data_uri)
  file.original_filename = name

  # Save the model  
  model.attachment = file
  model.save!
end

Source: https://stackoverflow.com/a/28036282/545017

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.