Search code examples
asp.netc#-4.0ckeditor

how to enable image upload with ckeditor with asp.net 4.0


I have serval of pages that accepts rich text. and I just use ckeditor control for that. it's loaded fine. but can't show image browser tab for image upload. here is my html markup :

<script type="text/javascript">
        $(function () {
            CKEDITOR.replace('<%=CKEditor1.ClientID %>', { filebrowserImageUploadUrl: 'Upload.ashx' });
        });
    </script>
<tr>
                                                <td style="width: 20%">Template Body
                                                </td>
                                                <td>
                                                    <CKEditor:CKEditorControl ID="CKEditor1" runat="server" ValidationGroup="AddNewTemplate" PasteFromWordRemoveFontStyles="False"></CKEditor:CKEditorControl>
                                                    <asp:CustomValidator ID="CustomValidator2" runat="server"
                                                        ErrorMessage="Template body is required."
                                                        OnServerValidate="CustomValidator2_ServerValidate"
                                                        ValidationGroup="AddNewTemplate" ForeColor="Red"></asp:CustomValidator>
                                                </td>
                                            </tr>

here is my upload ashx file for image upload.

<%@ WebHandler Language="C#" Class="Upload" %>

using System;
using System.Web;
using System.Configuration;
using System.IO;
public class Upload : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        HttpPostedFile uploads = context.Request.Files["upload"];
       string CKEditorFuncNum = context.Request["CKEditorFuncNum"];
       string file = DateTime.Now.ToString("ddMMyyyy_HHmmss") + Path.GetExtension(uploads.FileName);
       uploads.SaveAs(context.Server.MapPath(".") + "\\EmailImages\\" + file);
       //provide direct URL here
       string url = "/EmailImages/" + file;  
      context.Response.Write("<script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + url + "\");</script>");
      context.Response.End();                     
    }

    public bool IsReusable {
        get { return false; }
    }
}

and here is my screen of ckeditor without upload tab : enter image description here


Solution

  • Yah, One thing we can do with CKEditor control. Just See properties of CKeditor control which have on page. and just put Upload.ashx filename assign to this properties.

    enter image description here