FCKeditor是一個專門使用在網頁上屬於開放原始碼的所見即所得 (WYSIWYG)文字編輯器。它志於輕量化,不需要太複雜的安裝步驟即可使用。它能在asp、asp.net、PHP、JSP等多個平台下使用,並且支援大部分瀏覽器,所以在它出現的6年時間裡,成為了最流行的文字編輯器。
隨著技術的發展,很多更新更方便的東西為開發人員提供了更大的便利。在這種情況下,FCKeditor團隊推出了一個FCKeditor的重寫版本——CKEditor。
新版本的CKEditor載入速度更快、更方便使用,在新版本的基礎上又一個全新的使用者介面,甚至可以讓使用者精確地自訂色彩。CKEditor經過了重寫,提供了豐富而強大的整合和互動的API。該編輯器是完全基於外掛程式的,它可以擴充和所有組件以符合所有需求。
新版本的CKEditor只提供了基本的文本編輯功能,上傳模組由另一個組件CKFinder。如果同時需要上傳功能,就需要再下載CKFinder(http://www.ckfinder.com/)。
CKEditor的配置
下面我們看一下在asp.net環境中如何使用新版本的CKEditor和CKFinder。
CKEditor的配置相對FCKeditor來說非常簡單。將檔案夾拷貝到你的程式目錄,然後你只需要在頁面中添加js引用:
view plaincopy to clipboardprint?
- <mce:script type="text/javascript" src="ckeditor/ckeditor.js" mce_src="ckeditor/ckeditor.js"></mce:script>
- 然後在頁面中就能夠使用:
- <textarea class="ckeditor" cols="80" id="editor1" name="editor1" rows="10">
- 這裡是內容
<mce:script type="text/javascript" src="ckeditor/ckeditor.js" mce_src="ckeditor/ckeditor.js"></mce:script><br />然後在頁面中就能夠使用:<br /><textarea class="ckeditor" cols="80" id="editor1" name="editor1" rows="10"><br />這裡是內容<br />
這樣就可以使用了。
在asp.net下,如果想使用後台取得編輯器裡的資料,可以這樣來做:
view plaincopy to clipboardprint?
- <asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine"></asp:TextBox>
- <mce:script type="text/javascript"><!--
-
- CKEDITOR.replace( '<%= txtContent.ClientID %>' );
- // --></mce:script>
<asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine"></asp:TextBox><br /><mce:script type="text/javascript"><!--</p><p> CKEDITOR.replace( '<%= txtContent.ClientID %>' );<br />// --></mce:script>
<%= txtContent.ClientID %>是伺服器控制項被編譯過以後在HTML中顯示的ID名,這樣就可以通過後台調用編輯器中的資料了。
為了減少編輯器的大小,可以刪除一些不必要的檔案,如把_samples、_source、_tests三個檔案夾刪除,進入lang檔案目錄,保留en.js、zh.js、zh-cn.js三個檔案,其餘的語言檔案如果你用不到,可以刪除。
CKFinder的配置:
1、把解壓後的/ckfinder/夾拷貝到你的web目錄下;然後把bin目錄下的dll檔案拷到你自己的bin目錄下。
2、建立一個上傳檔案的目錄。在預設配置的情況下,/ckfinder/userfiles/是上傳目錄;如果想修改上傳目錄,修改config.ascx檔案中的BaseUrl = "/uploads/",這裡是以根目錄作為絕對路徑的目錄,注意以反斜線結尾。
3、確保你的檔案夾能被網路訪問寫入。在Windows裡,給IUSR_<ServerName>使用者寫入權限;CKFinder可以結合session來判斷使用者是否有許可權進行上傳。如果不需要進行使用者身分識別驗證,那麼修改config.ascx檔案裡的CheckAuthentication()函數,直接返回true。要進行身分識別驗證,也是修改這個函數。
4、編輯config.ascx檔案。確保你已經正確設定該檔案中的設定,並確定CKFinder在裡面。
然後測試是否可用,運行下面這個頁面:
/ckfinder/_samples/aspx/standalone.aspx
沒有意外的就,應該能使用了。
PS:網上一些教程說CKFinder需要付費才能上傳,那是放屁。未註冊的CKFinder照樣可以使用全部功能,只不過在編輯器裡放了個LOGO而已。
與CKEditor整合:
按照上面的配置好以後,理論上CKEditor和CKFinder都應該能使用了。但是這個時候CKEditor還不具備上傳功能,得把兩個東西整合在一起。代碼相當簡單:
view plaincopy to clipboardprint?
- <asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine"></asp:TextBox>
- <mce:script type="text/javascript"><!--
- // This call can be placed at any point after the
- // <textarea>, or inside a <head><script> in a
- // window.onload event handler.
-
- // Replace the <textarea id="editor"> with an CKEditor
- // instance, using default configurations.
- CKEDITOR.replace('<%= txtContent.ClientID %>',
- {
- filebrowserBrowseUrl: '../ckfinder/ckfinder.html',
- filebrowserImageBrowseUrl: '../ckfinder/ckfinder.html?Type=Images',
- filebrowserFlashBrowseUrl: '../ckfinder/ckfinder.html?Type=Flash',
- filebrowserUploadUrl: '../ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files',
- filebrowserImageUploadUrl: '../ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images',
- filebrowserFlashUploadUrl: '../ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash'
- }
- );
- // --></mce:script>
<asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine"></asp:TextBox><br /> <mce:script type="text/javascript"><!--<br /> // This call can be placed at any point after the<br /> // <textarea>, or inside a <head><script> in a<br /> // window.onload event handler.</p><p> // Replace the <textarea id="editor"> with an CKEditor<br /> // instance, using default configurations.<br /> CKEDITOR.replace('<%= txtContent.ClientID %>',<br /> {<br /> filebrowserBrowseUrl: '../ckfinder/ckfinder.html',<br /> filebrowserImageBrowseUrl: '../ckfinder/ckfinder.html?Type=Images',<br /> filebrowserFlashBrowseUrl: '../ckfinder/ckfinder.html?Type=Flash',<br /> filebrowserUploadUrl: '../ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files',<br /> filebrowserImageUploadUrl: '../ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images',<br /> filebrowserFlashUploadUrl: '../ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash'<br /> }<br /> );<br />// --></mce:script><br />
如果路徑沒有配置錯誤,那麼恭喜你,享受全新的CKEditor吧!