Annie
Annie

Reputation: 3190

Saving TinyMCE Base64 images with dragonfly

I am using tinymce-rails-imageupload plugin with dragonfly.

When the image is uploaded via separate form in popup window, it behaves as expected (save image in datastore).

But when the user drag-drop or paste image into TinyMCE, the imageupload plugin allows it. I tried to find a way to disable this behavior, but apparently there is no straightforward way to disable allowing image upload, while disallowing the past/drag-drop behavior. So I gave up on that..

Now, I'm trying to save BASE64 image in TinyMCE's content.

In controller:

def store_file
  @image = Resource.new :res_image => params[:file]
  @image.save
  render json: {
    image: {
      url: @image.res_image.remote_url
    }
  }, content_type: "text/html"
end

def create
  @entry = Entry.new(params[:entry])

  # iterate through tinyMCE field params[:entry][:message]
    # if image tag is found
      # if value of src tag starts with "data:"
        # then replace it with the output of
        # Resource.create_image_from_base64(extracted_base64_value)
      # end if
    # end if
  # end iteration

  begin
    @entry.save!
    flash[:success] = "Entry was successfully created."
    redirect_to entries_path
  rescue Mongoid::Errors::Validations => e
    render :action => "new"
  end
end

In Resource model, I would have something like:

image_accessor :res_image

field :res_image_uid,      type: String
field :res_image_name,     type: String

def create_image_from_base64(base_64_encoded_data)
  file = File.open('temp.png', 'wb') do|f|
   f.write(Base64.decode64(base_64_encoded_data))
  end

  resource = # create Resource with temp file

  file.close

  resource.res_image.remote_url
end

Questions:

Upvotes: 2

Views: 890

Answers (1)

Florian Eck
Florian Eck

Reputation: 495

Even if it is an old question:

look at this: https://groups.google.com/forum/#!topic/dragonfly-users/xNWIwZf5-_Y

Upvotes: 1

Related Questions