CKeditor is one of the most widely used WYSIWYG editors for web applications. Overtime, the CKeditor continued to evolve by adding new features that made HTML text editing a lot easier. When using a WYSIWYG editor, we will often need to upload image to server and embed it in the HTML content. By default, the CKeditor supports embedding an image that is already uploaded
or from an external source by providing its URL.
In this article, let’s see how we can upload an image to our website and embed it in CKeditor using the easy solution below. The CKeditor has a property called filebrowserImageUploadUrl which will provide an option to upload images to the server when configured. This property takes a file uploader (a page or a handler) url to upload the selected image to the server. The handler that is responsible for uploading the image should return back the URL of the image to display in the CKeditor. Once filebrowserImageUploadUrlproperty is configured, you will be able to see a new tab called “Upload”
in Image Properties pop-up of CKeditor.
Follow the below steps to integrate image upload functionality with CKEditor in ASP.NET. Here the solution.
- Create a New ASP.NET Website “CKeditorDemo”.
- Download CKEditor and extract in your web folder root.
- Create a new folder named “Images” in your web folder root.
- Add the new ASHX Handler file (.ashx) “Upload.ashx” and Copy Paste below code into “Upload.ashx”
<%@ WebHandlerLanguage="C#"Class="Upload"%>
using System;
using System.Web;
public class Upload : IHttpHandler{
public voidProcessRequest (HttpContext context) {
HttpPostedFile uploads = context.Request.Files["upload"];
string CKEditorFuncNum = context.Request["CKEditorFuncNum"];
string file = System.IO.Path.GetFileName(uploads.FileName);
uploads.SaveAs(context.Server.MapPath(".") + "\\Images\\"+ file);
string url = "http://localhost/CKeditorDemo/Images/"+ file;
context.Response.Write("<script>window.parent.CKEDITOR.tools.callFunction(
"+ CKEditorFuncNum +
", \"" + url + "\");</script>");
context.Response.End();
}
public boolIsReusable {
get { return false; }
}
}
- Call the script and declare Textbox with ID="txtCkEditor" in .aspx file
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckeditor/adapters/jquery.js"></script>
<script type="text/javascript">
$(function () {
CKEDITOR.replace('<%=txtCkEditor.ClientID %>',
{ filebrowserImageUploadUrl: '/CKeditorDemo/Upload.ashx' }); //path to "Upload.ashx"
});
</script>
<asp:TextBox ID="txtCkEditor" TextMode="MultiLine" runat="server"></asp:TextBox>
- You are done with the setting. Now run the website you will see the CKEditor configured in the page.
- Then choose the image icon in the CKEditor to upload the Image.
- Select the image by clicking Browse button in Upload tab and select “Send it to the Server” button to save the image in server.
- The uploaded image is displayed in the CKEditor after clicking “OK”.