Add Custom File Upload In CKEditor

 CKEditor allows us to add the images and videos in CKEditor content. If you want to add an image in an editor content, just put the URL of your image path in the image dialog popup -- but the real Web Applications user cannot do this. Thus, first upload the files to the Server, followed by the image path, which is automatically bound in an image tag. This post allows you to learn how to add your own fileuploader in CKEditor.

Without File Uploder

In this case, just put whole path of the image in the URL box and we can get the images inside CKEditor content.

 

With File Uploder

In this case, we connect our own File Uploder or Server and the image preview pops up with CKEditor dialog. Thus, the user can upload or select the files.

Step 1

Enable browse Server button.

You can see the above image doesn’t have any browse button in the image dialog. Thus, we must enable browse Server button to bind custom event. Thus, we add filebrowserBrowseUrl option in config.js.

  1. CKEDITOR.editorConfig = function( config ) {  
  2.   
  3. config.filebrowserBrowseUrl = 'javascript:void(0)';  
  4.   
  5. }  

Now, we can get Browse Server button. 

Step 2
 
Bind CKEditor dialog with Custom File Uploader

Add the codes given below to your CKEditor page.

  1. $(document).ready(function() {  
  2.     CKEDITOR.replace('myckeditor');  
  3.     CKEDITOR.on('dialogDefinition'function(ev) {  
  4.         //dialogDefinition is a ckeditor event it's fired when ckeditor dialog instance is called  
  5.         var dialogName = ev.data.name;  
  6.         var dialogDefinition = ev.data.definition;  
  7.         if (dialogName == 'image') { //dialogName is name of dialog and identify which dialog is fired.  
  8.             var infoTab = dialogDefinition.getContents('info'); // get tab of the dialog  
  9.             var browse = infoTab.get('browse'); //get browse server button  
  10.             browse.onClick = function() {  
  11.                 //here we can invoke our custom fileuploader or server files popup  
  12.                 alert('open your file uploader or server files popup');  
  13.             };  
  14.         }  
  15.     });  
  16. });  


Step 3
 
Insert path of an image to CKEditor

After uploading or selecting the file, we can get the full path of the image. Finally insert an image path into CKEditor dialog URL input box.

  1. //set image path to dialog  
  2.   
  3. var dialog = CKEDITOR.dialog.getCurrent();  
  4.   
  5. dialog.selectPage('info');  
  6.   
  7. var tUrl = dialog.getContentElement('info''txtUrl');  
  8.   
  9. tUrl.setValue("put value of image path");  

 

If you want to remove Browse Server Button from Link tab, just add the code given below inside dialogDefinition.

  1. if (dialogName == 'image') {  
  2.     var linkTab = dialogDefinition.getContents('Link');  
  3.     linkTab.remove('browse');  
  4. }  

For more about CKEditor, refer to:

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now