溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

CKEditor&CKFinder結(jié)合實(shí)現(xiàn)在線HTML編輯器

發(fā)布時(shí)間:2020-06-27 13:40:39 來源:網(wǎng)絡(luò) 閱讀:2680 作者:junweilv 欄目:編程語言

最近在做一個(gè)項(xiàng)目時(shí)需要用到在線HTML編輯器,之前做的是用的FreeTextBox,但總感覺不太完善。于是想起了FCKeditor,知道它很強(qiáng)大,但還沒用過,于是準(zhǔn)備使用FCKeditor。
FCKeditor目前已經(jīng)改名為了CKEditor和CKFinder,CKEditor不包含上傳文件功能,該功能被分割在了CKFinder中,因此需要同時(shí)使用這兩個(gè)文件才能實(shí)現(xiàn)文件上傳,同時(shí)因?yàn)槲沂腔贏SP.NET開發(fā)的,需要同時(shí)下載CKEditor for ASP.NET。
下面詳細(xì)介紹如下使用CKEditor&CKFinder實(shí)現(xiàn)在線HTML編輯器:

到官方下載相關(guān)文件(注意選擇asp.net版本):http://ckeditor.com/download ckeditor_3.6.2.zip ,ckeditor_aspnet_3.6.2.zip http://ckfinder.com/download ckfinder_aspnet_2.1.zip

我是在VS2010+SQL2008Express Win7 旗艦版的環(huán)境下開發(fā)的。
新建ASP.NET空網(wǎng)站,分別把下載的文件解壓下,復(fù)制解壓后的ckeditor_3.6.2里面的ckeditor整個(gè)文件夾粘貼到網(wǎng)站的根目錄下,然后刪除不用的文件,有童鞋稱這個(gè)過程為“瘦身”,呵呵
結(jié)果如下:
CKEditor&CKFinder結(jié)合實(shí)現(xiàn)在線HTML編輯器

同樣把解壓后的ckfinder_aspnet_2.1的文件夾里的ckfinder文件夾整個(gè)復(fù)制粘貼到網(wǎng)站的根目錄下,與ckeditor同級(jí)。也對(duì)其瘦身,結(jié)果如下:
CKEditor&CKFinder結(jié)合實(shí)現(xiàn)在線HTML編輯器

添加到CKEditor.NET.dll和CKFinder.dll文件的引用,分別位于下載的文件的bin目錄下。
接下來開始配置
先修改ckeditor,在ckeditor文件夾下打開config.js,添加如下代碼,分別設(shè)置皮膚,語言,工具條

CKEDITOR.editorConfig = function (config) {

    // Define changes to default configuration here. For example:

    // config.language = 'fr';

    // config.uiColor = '#AADC6E';

    config.skin = 'v2';

    config.language = 'zh-cn';

 

    config.toolbar = 'Full';

 

    config.toolbar_Full =

    [

             { name: 'document', items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },

             { name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },

             { name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },

             { name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] },

             '/',

             { name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },

             { name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },

             { name: 'links', items : [ 'Link','Unlink','Anchor' ] },

             { name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe' ] },

             '/',

             { name: 'styles', items : [ 'Styles','Format','Font','FontSize' ] },

             { name: 'colors', items : [ 'TextColor','BGColor' ] },

             { name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }

    ];

 

    config.toolbar_Basic =

    [

             ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','About']

    ];

};

現(xiàn)在就可以在aspx頁(yè)面添加ckeditor編輯器了,新建Default.aspx,在head部分添加js引用。<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
這個(gè)是必須的。
然后在div部分添加<asp:TextBox ID="txtContent" class="ckeditor" TextMode="MultiLine" runat="server"></asp:TextBox>,
保存運(yùn)行就可以看到編輯器了,
 

CKEditor&CKFinder結(jié)合實(shí)現(xiàn)在線HTML編輯器

獲取編輯器的值插入數(shù)據(jù)庫(kù) 則TextBox.Text即可,綁定的話直就從數(shù)據(jù)庫(kù)讀取<asp:TextBox ID="txtContent" class="ckeditor" TextMode="MultiLine" Text='<%# Bind("info") %>' runat="server"></asp:TextBox> ,提示:在插入類似'時(shí)候ASPX頁(yè)面提示客戶端(*)中檢測(cè)到有潛在危險(xiǎn)的 Request.Form 值。在頁(yè)面HTML代碼第一行加入validateRequest="false"或者修改web.config文件即可,但這不是最安全的做法,可能存在提交非法數(shù)據(jù)和跨站的危險(xiǎn)。關(guān)鍵是這時(shí)點(diǎn)擊插入圖片時(shí),出現(xiàn)的是

CKEditor&CKFinder結(jié)合實(shí)現(xiàn)在線HTML編輯器

沒有上傳圖片選項(xiàng)卡,雖然這樣也可以用,需要通過其他方法上傳圖片到服務(wù)器,然后粘貼進(jìn)圖片的URL地址。但顯然我們希望能夠直接在此進(jìn)行圖片的上傳和編輯。接下來就用到ckfinder了。
繼續(xù)修改ckeditor下面的config.js,在函數(shù)的底部繼續(xù)加入如下代碼:

config.filebrowserBrowseUrl = 'ckfinder/ckfinder.html';

    config.filebrowserImageBrowseUrl = 'ckfinder/ckfinder.html?Type=Images';

    config.filebrowserFlashBrowseUrl = 'ckfinder/ckfinder.html?Type=Flash';

    config.filebrowserUploadUrl = 'ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files';

    config.filebrowserImageUploadUrl = 'ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images';

    config.filebrowserFlashUploadUrl = 'ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash';

config.filebrowser* 為上傳文件時(shí)調(diào)用的ckfinder connector.aspx文件相應(yīng)路徑,根據(jù)您的網(wǎng)站文件結(jié)構(gòu)修改,僅為測(cè)試我沒有修改文件夾名稱。添加代碼后在運(yùn)行一下看看這個(gè)時(shí)候你發(fā)現(xiàn)有了上傳的按鈕

但這時(shí)當(dāng)上傳圖片時(shí),出現(xiàn)“因?yàn)榘踩?,文件不可瀏覽。請(qǐng)聯(lián)系系統(tǒng)管理員并檢查CKFinder配置文件。

這時(shí)打開ckfinder下面的config.ascx文件,修改public override bool CheckAuthentication()
返回值為true,這里需要添加過濾條件,因?yàn)槭菧y(cè)試就不寫了

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 true;

         }

還是在這個(gè)文件內(nèi)找到public override void SetConfig()
方法里面的BaseUrl = "/ckfinder/userfiles/";后面的目錄根據(jù)自己的項(xiàng)目結(jié)構(gòu)填寫,我這里用的是默認(rèn)的文件夾。
至此基本結(jié)束了。下一篇文章會(huì)介紹如何給上傳的圖片重命名。至于詳細(xì)情況可查看官方文檔。

本文參考:http://www.cnblogs.com/root7/archive/2010/11/02/1866712.html

附件:http://down.51cto.com/data/2359236
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI