ASP.NET中使用CKEditor3.2+CKFinder1.4.3(转载)

 今天偶然看到一篇关于 CKEditor与CKFinder整合文章,心想有一段时间没有使用这种东西了。于是乎自己动手亲自体验了一下,本以为简单但在东西编写的过程发现了很多没有遇见毛病。

所以记录一下自己东西编写除错的过程

首先,下载2个插件包

CKEditor下载地址:http://ckeditor.com/

CKFinder下载地址:http://ckfinder.com/

1.然后创建项目,将解压的文件夹拷贝到项目中,编写页面代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CKEditor._Default" ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <!--引用脚本文件-->
    <script type="text/javascript" language="javascript" src="ckeditor/ckeditor.js"></script>
    <script type="text/javascript" language="javascript" src="ckfinder/ckfinder.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:TextBox ID="Content" runat="server" TextMode="MultiLine" Height="250px" Width="500px"></asp:TextBox>
     <!--这句脚本代码必须放在文件后面-->
     <script type="text/javascript">
     CKEDITOR.replace('<%=Content.ClientID%>', {});
     var editor = CKEDITOR.replace('<%=Content.ClientID%>');
     CKFinder.SetupCKEditor(editor, '../ckfinder/');
    </script> 

    <asp:Literal ID="Literal1" runat="server" ></asp:Literal>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ok" />
    </div>
    </form>
</body>
</html>

2.配置CKEditor下的config.js文件代码如下:

CKEDITOR.editorConfig = function( config )
{
 // Define changes to default configuration here. For example:
 // config.language = 'fr';
    // config.uiColor = '#AADC6E';
   
    config.language = 'zh-cn'; //中文
    config.uiColor = '#54ADD8'; //编辑器颜色
    config.font_names = '宋体;楷体_GB2312;新宋体;黑体;隶书;幼圆;微软雅黑;Arial;Comic Sans MS;Courier New;Tahoma;Times New Roman;Verdana';
    config.toolbar_Full =
    [
        ['Source', '-', 'Preview', '-', 'Templates'],
        ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print', 'SpellChecker', 'Scayt'],
        ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
        ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
        '/',
        ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
        ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote', 'CreateDiv'],
        ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
        ['Link', 'Unlink', 'Anchor'],
          ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
        '/',
        ['Styles', 'Format', 'Font', 'FontSize'],
        ['TextColor', 'BGColor'],
        ['Maximize', 'ShowBlocks', '-', 'About']
    ];

    config.toolbar_Basic =
    [
        ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'About']
    ];

    config.width =771; //宽度

    config.height = 260; //高度
   
    //如果需要使用ckfinder上传功能必须添下列代码
    config.filebrowserBrowseUrl = location.hash + '/ckfinder/ckfinder.html';
    config.filebrowserImageBrowseUrl = location.hash + '/ckfinder/ckfinder.html?Type=Images';
    config.filebrowserFlashBrowseUrl = location.hash+'/ckfinder/ckfinder.html?Type=Flash';
    config.filebrowserUploadUrl = location.hash + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files';
    config.filebrowserImageUploadUrl = location.hash + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images';
    config.filebrowserFlashUploadUrl = location.hash + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash';

};

3.配置CKFinder下的Config.ascx文件:

首先配置下载权限

public override bool CheckAuthentication()
 {
  // WARNING : DO NOT simply return "true". By doing so, you are allowing
  // "anyone" to upload and list the files in your server. You must implement
  // some kind of session validation here. Even something very simple as...
  //
  //  return ( Session[ "IsAuthorized" ] != null && (bool)Session[ "IsAuthorized" ] == true );
  //
  // ... where Session[ "IsAuthorized" ] is set to "true" as soon as the
  // user logs on your system.
        // return fase;
  return true;
 }

其次配置Config.ascx服务器文件路径,用于存储图片的文件夹
 BaseUrl = " ~/ckfinder/userfiles/";

该路径根据实际情况不同,设置也不同。

4.引用CKFinder文件中bin文件下的Release中ckfinder.dll否则会有错误。

至此配置已经完成。

 注意事项:

1.运行的时候,可能出现例如:System.Web.HttpRequestValidationException: 从客户端(Content="<p>fdgdfgdfg</p>...")中检测到有潜在危险的 Request.Form 值的错误,

该错误需要在页面page标签中添加validateRequest="false".

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CKEditor._Default" ValidateRequest="false" %>

2.编译的时候如果提示我AssemblyTitle、AssemblyCompany等属性重复,该错误可能是ckeditor,ckfinder示例代码中的AssemblyInfo.cs文件存在冲突,删除示例代码 source文件或者samples文件中的代码即可。

 

 

 

 

 

 

 

 

 

 

posted @ 2010-03-11 16:41  狗狗听话  阅读(936)  评论(0编辑  收藏  举报