Donations
How does the image upload work?
Before you can create your custom upload adapter, you should learn about the image upload process in CKEditorÃÆââ¬Å¡Ãâà5. The whole process consists of the following steps:
First, an image (or images) needs to get into the rich-text editor content. There are many ways to do that, for instance:
- pasting an image from the clipboard,
- dragging a file from the file system,
- selecting an image through a file system dialog.
The images are intercepted by theÃÆââ¬Å¡Ãâàimage uploadÃÆââ¬Å¡Ãâàplugin (which is enabled in all officialÃÆââ¬Å¡Ãâàeditor builds).
For every image, the image upload pluginÃÆââ¬Å¡Ãâàcreates an instance of a file loader.
- The role of theÃÆââ¬Å¡Ãâàfile loaderÃÆââ¬Å¡Ãâàis to read the file from the disk and upload it to the server by using the upload adapter.
- The role of theÃÆââ¬Å¡Ãâàupload adapterÃÆââ¬Å¡Ãâàis, therefore, to securely send the file to the server and pass the response from the server (for example, the URL to the saved file) back to the file loader (or handle an error, if there was one).
While the images are being uploaded, the image upload plugin:
- Creates placeholders for these images.
- Inserts them into the editor.
- Displays the progress bar for each of them.
- When an image is deleted from the editor content before the upload finishes, it aborts the upload process.
Once the file is uploaded, the upload adapter notifies the editor about this fact by resolving itsÃÆââ¬Å¡ÃâÃÂ
Promise
. It passes the URL (or URLs in case of responsive images) to the image upload plugin which replaces theÃÆââ¬Å¡ÃâÃÂsrc
ÃÆââ¬Å¡ÃâàandÃÆââ¬Å¡ÃâÃÂsrcset
ÃÆââ¬Å¡Ãâàattributes of the image placeholder in the editor content.
This is just an overview of the image upload process. Actually, the whole thing is more complicated. For instance, images can be copied and pasted within the WYSIWYG editor (while the upload takes place) and all potential upload errors must be handled, too. The good news is these tasks are handled transparently by theÃÆââ¬Å¡Ãâàimage uploadÃÆââ¬Å¡Ãâàplugin so you do not have to worry about them.
To sum up, for the image upload to work in the rich-text editor, two conditions must be true:
TheÃÆââ¬Å¡Ãâàimage uploadÃÆââ¬Å¡Ãâàplugin must be enabledÃÆââ¬Å¡Ãâàin the editor. It is enabled by default in all officialÃÆââ¬Å¡Ãâàbuilds, but if you areÃÆââ¬Å¡ÃâàcustomizingÃÆââ¬Å¡ÃâàCKEditorÃÆââ¬Å¡Ãâà5, do not forget to include it.
The upload adapter needs to be defined. This can be done by using (enablingÃÆââ¬Å¡ÃâàandÃÆââ¬Å¡Ãâàconfiguring):
- One of the existing upload adapters.
- Your custom upload adapterÃÆââ¬Å¡Ãâàand handling uploaded files on your server backend.
The anatomy of the adapter
A custom upload adapter allows you to takeÃÆââ¬Å¡Ãâàfull controlÃÆââ¬Å¡Ãâàover the process of sending the files to the server as well as passing the response from the server back to the rich-text editor.
Any upload adapter, whether an image upload adapter or a generic file upload adapter, must implement theÃÆââ¬Å¡ÃâàUploadAdapter
ÃÆââ¬Å¡ÃâàinterfaceÃÆââ¬Å¡Ãâàto work, that is, it must bring its ownÃÆââ¬Å¡Ãâàupload()
ÃÆââ¬Å¡ÃâàandÃÆââ¬Å¡Ãâàabort()
ÃÆââ¬Å¡Ãâàmethods.
- TheÃÆââ¬Å¡ÃâÃÂ
upload()
ÃÆââ¬Å¡Ãâàmethod must return a promise:- resolvedÃÆââ¬Å¡Ãâàby a successful upload with an object containing information about the uploaded file (see the section aboutÃÆââ¬Å¡Ãâàresponsive imagesÃÆââ¬Å¡Ãâàto learn more),
- rejectedÃÆââ¬Å¡Ãâàbecause of an error, in which case nothing is inserted into the content.
- TheÃÆââ¬Å¡ÃâÃÂ
abort()
ÃÆââ¬Å¡Ãâàmethod must allow the editor to stop the upload process. It is necessary, for instance, when the image was removed from the content by the user before the upload finished or the editor instance wasÃÆââ¬Å¡Ãâàdestroyed.
In its simplest form, a custom adapter implementing theÃÆââ¬Å¡ÃâàUploadAdapter
ÃÆââ¬Å¡Ãâàinterface will look as follows. Note thatÃÆââ¬Å¡Ãâàserver.upload()
,ÃÆââ¬Å¡Ãâàserver.onUploadProgress()
ÃÆââ¬Å¡ÃâàandÃÆââ¬Å¡Ãâàserver.abortUpload()
ÃÆââ¬Å¡Ãâàshould be replaced by specific implementations (dedicated for your application) and onl
Actualités
TémoignagesNulla vel viverra auctorleo magna sodales felis, quis malesuada nibh odio ut
Sandra Wood
Campagnes de collecte de fonds en cours
-
Rerum aut aut ipsum quia accusantium et.
-
Porro praesentium excepturi cum autem tempora aliquid.
-
Cumque tenetur minus molestiae unde et inventore deserunt quis.
-
Sit autem ut illo corporis nostrum.
-
Non temporibus ipsum aut aut assumenda voluptatem error.
-
Delectus accusantium vitae iusto ipsum.
-
Ut blanditiis et magnam rerum inventore voluptate.
-
Sint voluptatum repellat et consequatur.
-
Minima commodi error tempora commodi adipisci distinctio est.