现在的位置: 首页 > 综合 > 正文

CKEditor在asp.net上使用的图例详解

2012年11月20日 ⁄ 综合 ⁄ 共 6846字 ⁄ 字号 评论关闭

  去年的时候自己用过CKEditor,感觉自己掌握的不怎么好,今天重新好好地学习研究一下,发现很多问题,在IE10上CKEditor的工具栏编辑器不再显示了,不管怎么弄就是那样,结果查询了一下,IE10对CKEditor有兼容性问题。结果自己在Chrome浏览器上试了下,没有问题,呵呵,原来是IE依旧需要发展啊。

  不管怎么说吧,今天把自己学习的详细步骤提供给大家参考,有什么问题,请留言。

  我的代码源文件,CKEditor和CKFinder的下载http://download.csdn.net/detail/heikeyuit/5476719

                      或者:http://ishare.iask.sina.com.cn/f/37149751.html

  1、官方网站(http://cksource.com)上下载获得CKEditor和CKFinder的最新版。这里是我上传的我是用的版本及例子。

  2、两个文件夹东西真的是很多,内容很全面,但是我们用的的东西不是全部,所以是挑选我们需要的东西添加到项目中去。这个是项目中CKEditor和CKFinder包含文件的截图

  3、在前台添加代码

<head runat="server">
    <title></title> 
    <script src="JS/ckedit/ckeditor.js" type="text/javascript"></script>  <!--必写的东西-->
    <style type="text/css">
        .ckeditor
        {}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" 
            CssClass="ckeditor" Height="207px" Width="517px"></asp:TextBox>
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="显示" /> <!--按钮点击 后台产生的事件-->
    </form>
</body>

  4、CKEditor 本身不自带上传功能,所以需要配合CKFinder才可以实现上传。

    (1)、项目添加引用CKFinder.dll

    (2)、配置CKEditor的config.js (目录:/CKEditor/config.js ) 在CKEDITOR.editorConfig函数里加上,不需要的功能可以去掉

      代码如下:

/*
Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

CKEDITOR.editorConfig = function (config) {
    // Define changes to default configuration here. For example:
    // config.language = 'fr';
    // config.uiColor = '#AADC6E';

    var ckfinderPath = "/JS";   //注意这个地方的问题,JS是包含CKEditor和CKFinder的文件夹
    config.filebrowserBrowseUrl = ckfinderPath + '/ckfinder/ckfinder.html';
    config.filebrowserImageBrowseUrl = ckfinderPath + '/ckfinder/ckfinder.html?type=Images';
    config.filebrowserFlashBrowseUrl = ckfinderPath + '/ckfinder/ckfinder.html?type=Flash';
    config.filebrowserUploadUrl = ckfinderPath + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files';
    config.filebrowserImageUploadUrl = ckfinderPath + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images';
    config.filebrowserFlashUploadUrl = ckfinderPath + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash';
};

配置完成后CKEditor 就带有上传功能了,但假如上传图片,flash,以及其他文件时,文件如果用原来文件的名字,可能会出现重名的问题,

所以就要将文件名改为随机文件名。

  5、修改CKFinder的源码。CKFinder自带有源码,目录:/CKFinde/_source,打开项目,

    (1)、 打开/Settings/ConfigFile.cs文件,修改的地方,请看特殊标记

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace CKFinder.Settings
{
    public class ConfigFile : System.Web.UI.UserControl
    {
        public string LicenseName;
        public string LicenseKey;
        public string BaseUrl;
        public string BaseDir;
        public bool RandomReName; //随机重命名
        public bool SecureImageUploads;
        public bool ForceSingleExtension;
        public bool CheckDoubleExtension;
        public bool CheckSizeAfterScaling;
        public bool DisallowUnsafeCharacters;
        public string[] HtmlExtensions;
        public string[] Plugins;
        public Hashtable PluginSettings;

        public string DefaultResourceTypes;

        private Thumbnails _Thumbnails;
        private Images _Images;
        private AccessControlManager _AccessControl;
        private ResourceTypeManager _ResourceType;

        private string[] _HideFolders;
        private string[] _HideFiles;

        internal Regex HideFoldersRegex;
        internal Regex HideFilesRegex;

        public string RoleSessionVar;

        private static ConfigFile _Current;

        public ConfigFile()
        {
            _Thumbnails = new Thumbnails();
            _Images = new Images();
            _AccessControl = new AccessControlManager();
            _ResourceType = new ResourceTypeManager();

            this.HideFolders = new string[ 0 ];
            this.HideFiles = new string[ 0 ];

            LicenseName = "";
            LicenseKey = "";
            BaseUrl = "/ckfinder/userfiles/";
            BaseDir = "";
            RandomReName = true;
            ForceSingleExtension = true;
            CheckSizeAfterScaling = true;
            DisallowUnsafeCharacters = true;
            CheckDoubleExtension = true;
            DefaultResourceTypes = "";
            HtmlExtensions = new string[ 0 ];
            Plugins = new string[ 0 ];
            PluginSettings = new Hashtable();
            RoleSessionVar = "";
        }
    /*-----后面内容已经省略------------*/
} }

    (2)、打开/Connector/Config.cs文件,

      定位60行左右,添加一个属性:

      

      public bool RandomReName

      {

        get { return Settings.ConfigFile.Current.RandomReName; }

      }

 

    (3)、打开/Connector/CommandHandlers/FileUploadCommandHandler.cs文件,添加一句判断代码,下面显示的是部分代码,添加的代码已经标注

      

using System;
using System.Web;
using System.Text.RegularExpressions;

namespace CKFinder.Connector.CommandHandlers
{
    public class FileUploadCommandHandler : CommandHandlerBase
    {
        public FileUploadCommandHandler()
            : base()
        {
        }
        public static string sFileName=null;
        public override void SendResponse( System.Web.HttpResponse response )
        {
            int iErrorNumber = 0;
            //string sFileName = "";
            string sFilePath = "";
            string sUnsafeFileName = "";

            try
            {
                this.CheckConnector();
                this.CheckRequest();

                if ( !this.CurrentFolder.CheckAcl( AccessControlRules.FileUpload ) )
                {
                    ConnectorException.Throw( Errors.Unauthorized );
                }

                HttpPostedFile oFile = null;
                if ( HttpContext.Current.Request.Files["upload"] != null )
                {
                    oFile = HttpContext.Current.Request.Files["upload"];
                }
                else if ( HttpContext.Current.Request.Files["NewFile"] != null )
                {
                    oFile = HttpContext.Current.Request.Files["NewFile"];
                }
                else if ( HttpContext.Current.Request.Files.AllKeys.Length > 0 )
                {
                    oFile = HttpContext.Current.Request.Files[HttpContext.Current.Request.Files.AllKeys[0]];
                }

                if ( oFile != null )
                {
                    sFileName = oFile.FileName;

                    if ( Config.Current.CheckDoubleExtension )
                        sFileName = this.CurrentFolder.ResourceTypeInfo.ReplaceInvalidDoubleExtensions( sFileName );

                    sUnsafeFileName = sFileName;
                    
                    if ( Config.Current.DisallowUnsafeCharacters )
                        sFileName = sFileName.Replace(";","_");

                    // Replace dots in the name with underscores (only one dot can be there... security issue).
                    if ( Config.Current.ForceSingleExtension )
                        sFileName = Regex.Replace( sFileName, @"\.(?![^.]*$)", "_", RegexOptions.None );

                    if ( sFileName != sUnsafeFileName )
                        iErrorNumber = Errors.UploadedInvalidNameRenamed;

                    if ( Connector.CheckFileName( sFileName ) && !Config.Current.CheckIsHiddenFile( sFileName ) )
                    {
                        if ( !Config.Current.CheckSizeAfterScaling && this.CurrentFolder.ResourceTypeInfo.MaxSize > 0 && oFile.ContentLength > this.CurrentFolder.ResourceTypeInfo.MaxSize )
                            ConnectorException.Throw( Errors.UploadedTooBig );

                        string sExtension = System.IO.Path.GetExtension( sFileName );
                        sExtension = sExtension.TrimStart( '.' );
                        if (Config.Current.RandomReName)  //使用随机名
                        {
                            sFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + sExtension;
                        }
            /*剩下的部分代码已经省略,详细代码请查看我的项目代码,下载地址为*/
          }
        }
      } } }

    4) 重新生成项目,在bin文件夹中,找到CKFinder.dll,对于第一个项目重新添加对于CKFinder.dll的引用,最后一步:打开/ckfinder/config.ascx

   在SetConfig()中,添加一属性:(其实这个加不加都可以的,因为之前有设置默认值,但使用原名时一定要设置为false)

    RandomReName = true;//上传完毕后使用随机文件名

好了,到此已经配置成功了。我的代码源文件,CKEditor和CKFinder的下载地址http://download.csdn.net/detail/heikeyuit/5476719

                                或者:http://ishare.iask.sina.com.cn/f/37149751.html

给大家秀秀我的项目的显示效果

  6,配置成功后,我发现了,如果我想在我的项目中,查询我上传的文件的路径,1种方法是根据自己的配置项BaseUrl,,然后在加上部分路径,就可以查询到自己上传文件的路径,但是这个真的不是很好;第二种方法是对示例进行修改,然后,添加对其的引用,然后添加对于函数或属性的调用,但是这样的工作量很大啊,我最后跳过来,调过去,最后感觉别人封装的就是有一定的道理的,如果改变的部分代码的访问权限,可能以后使用就会对于代码的重要部分进行错误性的修改,这样很不好啊,所以,如果大家真的想调用里面的代码,我希望大家使用程序集-反射,这个方法真的是很有效啊,不会更改到程序集的内容啊。

好了,闲话少说了,现在就是希望大家关于IE与CKEditor的兼容性,提供很好地解决方案。

 我的代码源文件,CKEditor和CKFinder的下载地址http://download.csdn.net/detail/heikeyuit/5476719

                      或者:http://ishare.iask.sina.com.cn/f/37149751.html

抱歉!评论已关闭.