1. 程式人生 > >在ASP.NET中使用KindEditor富文本編輯器

在ASP.NET中使用KindEditor富文本編輯器

text java cti rto about pri limit utf-8 document

我以前一直用百度的UEditor。這次客戶提了一個需求要在編輯器中插入Flash動畫,但是不知道怎麽用UEditor實現,於是選用了KindEditor。

更重要的一點是,客戶的網站使用Framework2.0,但是UEditor只支持4.0或更高的版本(舊版本很難找到了)。

下面講一下使用KindEditor的步驟

1、首先到官方網站下載最新版的UEditor。

下載完成後解壓,目錄結構如下

技術分享圖片

可以看到,EEditor支持各種後端語言進行開發,由於我們使用的是ASP.NET,所以打開ASP.NET文件夾。

技術分享圖片

這裏有兩個很重要的文件file_manager_json.ashx和upload_json.ashx,他們用來負責處理客戶端的文件上傳請求。bin目錄中有個LitJSON.dll類庫,用來對對象進行序列化和反序列化操作。

2、將UEditor引用到項目中

<link href="/Js/KindEditor/themes/default/default.css" rel="stylesheet" charset="utf-8" type="text/css" />
<script src="/Js/KindEditor/kindeditor-all.js" charset="utf-8" type="text/javascript"></script>
<script src="/Js/KindEditor/lang/zh-CN.js" charset="utf-8" type="text/javascript"></script>

3、初始化UEditor

首先要做一些準備工作,在html代碼中添加一個textarea用來當UEditor的容器。

<div style="margin:0 auto;width:1000px;">
<
textarea id="content1" cols="100" rows="8" style="width:700px;height:200px;visibility:hidden;" runat="server"></textarea>
</
div> <div id="articlefoot" style="margin-top:20px;text-align:right"
> <input type="button" id="canceldoc" value="取消編輯" style="margin-right:20px;height:30px" /> <input type="button" id="savedoc" value="保存文檔" style="margin-right:20px;height:30px"/> </div>

然後在JS中對KindEditor進行初始化設置

KindEditor.ready(
     function (K) {
            editor = K.create(‘#content1‘, {
                //上傳處理程序的路徑
                uploadJson: ‘/js/KindEditor/asp.net/upload_json.ashx‘,
                imageSizeLimit: ‘10MB‘, //批量上傳圖片單張最大容量
                imageUploadLimit : 30, //批量上傳圖片同時上傳最多個數
                //文件管理處理程序的路徑
                fileManagerJson: ‘/js/KindEditor/asp.net/file_manager_json.ashx‘,
                allowFileManager: true,
                //要取值設置這裏 這個函數就是同步KindEditor的值到textarea文本框
                afterCreate: function () {
                    var self = this;
                    K.ctrl(document, 13, function () {
                        self.sync();
                        K(‘form[name=example]‘)[0].submit();
                    });
                    K.ctrl(self.edit.doc, 13, function () {
                        self.sync();
                        K(‘form[name=example]‘)[0].submit();
                    });
                },
                //上傳後執行的回調函數,獲取上傳圖片的路徑
                afterUpload: function (data) {
                    alert(data);
                },
                //同時設置這裏  
                afterBlur: function () {
                    this.sync();
                },
                width: ‘1000px;‘,
                height: ‘500px;‘,
                //編輯工具欄
                items: [
                ‘source‘, ‘|‘, ‘undo‘, ‘redo‘, ‘|‘, ‘preview‘, ‘print‘, ‘template‘, ‘code‘, ‘cut‘, ‘copy‘, ‘paste‘,
                ‘plainpaste‘, ‘wordpaste‘, ‘|‘, ‘justifyleft‘, ‘justifycenter‘, ‘justifyright‘,
                ‘justifyfull‘, ‘insertorderedlist‘, ‘insertunorderedlist‘, ‘indent‘, ‘outdent‘, ‘subscript‘,
                ‘superscript‘, ‘clearhtml‘, ‘quickformat‘, ‘selectall‘, ‘|‘, ‘fullscreen‘, ‘/‘,
                ‘formatblock‘, ‘fontname‘, ‘fontsize‘, ‘|‘, ‘forecolor‘, ‘hilitecolor‘, ‘bold‘,
                ‘italic‘, ‘underline‘, ‘strikethrough‘, ‘lineheight‘, ‘removeformat‘, ‘|‘, ‘image‘, ‘multiimage‘,
                ‘flash‘, ‘media‘, ‘insertfile‘, ‘table‘, ‘hr‘, ‘emoticons‘, ‘baidumap‘, ‘pagebreak‘,
                ‘anchor‘, ‘link‘, ‘unlink‘, ‘|‘, ‘about‘
                ]
            });
        });

在ASP.NET中使用KindEditor富文本編輯器