• [ckeditor系列]CKeditor自定义上传图片功能


    CKeditor可以配合CKfinder实现文件的上传及管理。但是往往我们上传的图片需要某些自定义的操作,比如将图片路径写入数据库,图片加水印等等操作。

    实现原理:配置CKeditor的自定义图标,单击弹出一个子窗口,在在子窗口中上传图片实现我们的自己的功能,然后自动关闭子窗口将图片插入到CKeditor的当前光标位置。

    实现步骤:

    1、配置CKeditor。网上很多资料,大家自己查。

    2、配置config.js文件。此文件为CKeditor的配置文件。配置需要显示的图标。

    CKEDITOR.editorConfig = function( config )
     {
         // Define changes to default configuration here. For example:
         config.language = 'zh-cn';
         config.skin = 'v2';
         // config.uiColor = '#AADC6E';
         config.toolbar =
         [
            ['Source', '-', 'Preview', '-'],
             ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord'],
             ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],        
             '/',
             ['Bold', 'Italic', 'Underline'],
             ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent'],
             ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
             ['Link', 'Unlink', 'Anchor'],
             ['addpic','Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],//此处的addpic为我们自定义的图标,非CKeditor提供。
             '/',
             ['Styles', 'Format', 'Font', 'FontSize'],
             ['TextColor', 'BGColor'],
            
         ];
     
         config.extraPlugins = 'addpic';
     
     };

     3、在CKeditor\plugins文件夹下新建addpic文件夹,文件夹下添加addpic.JPG图标文件,建议尺寸14*13。addpic.JPG图标文件即为显示在CKeditor上的addpic的图标。在图标文件同目录下添加文件plugin.js,内容如下。

    (function () {
         //Section 1 : 按下自定义按钮时执行的代码
         var a = {
             exec: function (editor) {
                 show();
             }
         },
         b = 'addpic';
         CKEDITOR.plugins.add(b, {
             init: function (editor) {
                 editor.addCommand(b, a);
                 editor.ui.addButton('addpic', {
                     label: '添加图片',
                     icon: this.path + 'addpic.JPG',
                     command: b
                 });
             }
         });
     })();

     文件中的show函数为显示子页面使用,我将它写在CKeditor所在的页面中。

    4、edit.aspx页面使用的js

    edit.aspx页面就是使用CKeditor的页面。

    function show() {
    
        $("#ele6")[0].click();
    }
    function upimg(str) {
        if (str == "undefined" || str == "") {
            return;
        }
        str = "<img src='/html/images/" + str+"'</img>";
        CKEDITOR.instances.TB_Content.insertHtml(str);
        close();
    }
    function close() {
        $("#close6")[0].click();
    }
    $(document).ready(function () {
        new PopupLayer({ trigger: "#ele6", popupBlk: "#blk6", closeBtn: "#close6", useOverlay: true, offsets: { x: 50, y: 0} });
    });

    以上就是js的代码,弹出窗口是使用jquery的弹出层,弹出层中嵌套iframe,iframe中使用upimg.aspx上传页面,大家如果有其他需要可以自己去设计弹出效果。为了大家调试方便,我将我实现弹出层的代码贴出来。

    弹出层效果使用的是popup_layer.js方案,需要进一步加工的朋友可以自己在百度中谷歌。ele6为弹出激发需要的层,blk6为弹出层主体,close6为层中承载关闭按钮的层。代码如下

    <div id="ele6" style="cursor:hand; color: blue; display:none;"></div>
        <div id="blk6" class="blk" style="display:none;">
            <div class="head"><div class="head-right"></div></div>
            <div class="main">                
                <a href="javascript:void(0)"  id="close6" class="closeBtn"></a>                
                <iframe src="upimg.aspx"></iframe>                    
            </div>            
        </div>

    别忘记引用jquery和popup_layer.js。

    5、upimg.aspx页面

    aspx代码

    <div>
            <asp:FileUpload ID="FU_indexIMG" runat="server" />
            <br />
        <asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="取消" />
    </div>

    对应的cs代码

    protected void Button1_Click(object sender, EventArgs e)
         {
             string imgdir = UpImg();        
             script = "window.parent.upimg('" + imgdir + "');";
             ResponseScript(script);
         }
         protected void Button2_Click(object sender, EventArgs e)
         {
             string script = "window.parent.close();";
             ResponseScript(script);
         }
     /// <summary>
         /// 输出脚本
         /// </summary>
         public void ResponseScript(string script)
         {
             System.Text.StringBuilder sb = new System.Text.StringBuilder("<script language='javascript' type='text/javascript'>");
             sb.Append(script);
             sb.Append("</script>");
             ClientScript.RegisterStartupScript(this.GetType(), RandomString(1), sb.ToString());
         }
         /// <summary>
         /// 获得随机数
         /// </summary>
         /// <param name="count">长度</param>
         /// <returns></returns>
         public static string RandomString(int count)
         {
             RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
             byte[] data = new byte[count];
             rng.GetBytes(data);
             return BitConverter.ToString(data).Replace("-", "");
         }

    Button1_Click为确定按钮的单击事件函数。其中使用UpImg函数实现上传图片文件,我还在其中实现了加水印,缩图,将图片文件的大小以及相对路径存入数据库等自定义操作,大家可以在此发挥。UpImg返回值为保存图片的相对路径,然后调用editer.aspx页面的js函数upimg。js函数upimg功能为将字符串插入到CKeditor的当前光标位置,插入的是html代码。至此为止带有新上传图片相对路径的img标签就插入CKeditor的编辑区了,能够显示图片了。

    Button1_Click为取消按钮的单击事件函数。调用editer.aspx页面的js函数close,将弹出层隐藏。

  • 相关阅读:
    nginx 命令
    nginx 配置文件(windows)
    nginx 配置文件(linux)
    nginx 安装
    什么是REST架构
    名词解释
    建造者模式
    单例模式
    工厂模式
    赋值运算符,拷贝构造函数,clone()方法总结
  • 原文地址:https://www.cnblogs.com/101rico/p/2868391.html
Copyright © 2020-2023  润新知