Add Custom File Upload In CKEditor

Introduction

 
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 Uploader
 
In this case, just put the whole path of the image in the URL box and we can get the images inside CKEditor content.
 
 
With File Uploader
 
In this case, we connect our own File Uploader or Server and the image preview pops up with CKEditor dialog. Thus, the user can upload or select the files.
 
Step 1
 
Enable browse the Server button.
 
You can see the above image doesn’t have any browse button in the image dialog. Thus, we must enable the browse Server button to bind custom events. 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 the Browse Server button.
 
browseserver
 
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 the 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:
Next Recommended Reading Useful Methods And Events In CKEditor