当前位置:编程学习 > C#/ASP.NET >>

asp.net中配置与使用CKEditor编辑器图例

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;//上传完毕后使用随机文件名

好了,到此已经配置成功了

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,