Question:
Is there a way to turn off image upload and force the images (at least pasted images) to be converted to the base64 version so I can store them in my DB?
Answer:
Using the image.beforeUpload event, the images can be inserted as base64 instead of being uploaded to the server. Below is how that can be achieved:
new FroalaEditor('.selector', {
events: {
"image.beforeUpload": function(files) {
var editor = this;
if (files.length) {
// Create a File Reader.
var reader = new FileReader();
// Set the reader to insert images when they are loaded.
reader.onload = function(e) {
var result = e.target.result;
editor.image.insert(result, null, null, editor.image.get());
};
// Read image as base64.
reader.readAsDataURL(files[0]);
}
editor.popups.hideAll();
// Stop default upload chain.
return false;
}
}
})
Do not skip reading this:
Although inserting images as base64 can be easily achieved, our recommendation is to use images that are loaded from an URL. This might be tempting at a first glance because you don't have to deal with all the image storage, but you will get in troubles quickly. Here are the top 2 reasons for doing that when you're using a WYSIWYG HTML editor and
- browser tend to slow down when images are inserted as base64 because they have to process very large chunks of HTML;
- when you save the content, you'll pass a lot of data to your server and that would result in an unwanted overload.