Skip to content

Instantly share code, notes, and snippets.

@Martin91
Created January 27, 2015 07:35
Show Gist options
  • Select an option

  • Save Martin91/3a958394e84f82f2158f to your computer and use it in GitHub Desktop.

Select an option

Save Martin91/3a958394e84f82f2158f to your computer and use it in GitHub Desktop.
save base64 file as uploaded
class XXXController < ApplicationController
def create
moment = Moment.new photo: preprocess_uploaded_photo
# ...
end
def preprocess_uploaded_photo
encoded_photo_data = params[:moment][:photo]
image_data = Base64.decode64(encoded_photo_data['data:image/png;base64,'.length .. -1])
tempfile = Tempfile.new ['upload', 'png']
tempfile.binmode
tempfile.write(image_data)
ActionDispatch::Http::UploadedFile.new(tempfile: tempfile,
filename: 'upload.png')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment