2
我正在用蜻蜓的tinymce-rails-imageupload
插件。用蜻蜓保存TinyMCE Base64图像
当图像通过弹出窗口中的单独表单上载时,其行为与预期相同(将图像保存在数据存储区中)。
但是,当用户将图像拖放或粘贴到TinyMCE时,imageupload插件允许它。我试图找到一种方法来禁用这种行为,但显然没有直接的方法来禁止允许图片上传,同时禁止过去/拖放行为。所以我放弃了..
现在,我试图在TinyMCE的内容中保存BASE64图像。
在控制器:
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
在资源模型,我会是这样的:
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
问题:
如何创建 “条目以文件”?
有没有更好的方法来处理TinyMCE与蜻蜓粘贴/拖拽的base64图像?