1、CKEditor原名FckEditor,著名的HTML編輯器,可以在線編輯HTML內容。自己人用CKEditor,網友用UBBEditor。
配置參考文檔,主要將ckeditor中的(adapters、images、lang、plugins、skins、themes、ckeditor.js、config.js、contents.css)解壓到js目錄,然后“顯示所有文件”,將ckeditor的目錄“包含在項目中”,在發帖頁面引用ckeditor.js,然后設置多行文本框的class="ckeditor"(CSS強大)(服務端控件CssClass=" ckeditor ",客戶端控件要設定cols、rows屬性,一般不直接用html控件),代碼中仍然可以通過TextBox控件的Text屬性來訪問編輯器內容。
由於頁面提交的時候asp.net會把富文本編輯器中的html內容當成攻擊內容,因此需要在aspx中的Page標簽中設置 ValidateRequest="false" 來禁用攻擊檢測(2010中還要根據報錯信息修改WebConfig來禁用XSS檢測)。
遇到錯誤如下:
**修改WebConfig來禁用XSS檢測
當asp.net提交“<>”這些字符到aspx頁面時,如果沒有在文件頭中加入“ValidateRequest="false"”這句話,就會出現出錯提示:從客戶端(<?xml version="...='UTF-8'?><SOAP-ENV:Envelope S...")中檢測到有潛在危險的Request.Form 值。
如你是vs2008的用戶,只要在aspx文件的開始部分,如下文所示處:
<%@ Page Language="C#" CodeBehind="News_add.aspx.cs" Inherits="CKEditor.Default" %>
加上ValidateRequest="false" 即可。
但是如果是VS2010,僅僅這樣還是不夠的。還需要雙擊打開web.config,在<system.web></system.web>之間添加下面語句
<pages validateRequest="false" /> <httpRuntime requestValidationMode="2.0" />
2、CKFinder是一個CKEditor插件,用來為CKEditor提供文件的上傳的功能。將bin\Release下的CKFinder.dll添加到項目的引用;將core、ckfinder.js、ckfinder.html、config.ascx解壓到CKFinder自己的目錄。按照文檔修改CKEditor的config.js,將上傳的處理程序設定為CKFinder,注意路徑的問題。
1 CKEDITOR.editorConfig = function( config ) 2 { 3 // Define changes to default configuration here. For example: 4 // config.language = 'fr'; 5 // config.uiColor = '#AADC6E'; 6 7 //改成ckfinder的絕對路徑,從網站的本目錄開始 8 var ckfinderPath = "/admin/js"; 9 config.filebrowserBrowseUrl = ckfinderPath + '/ckfinder/ckfinder.html'; 10 config.filebrowserImageBrowseUrl = ckfinderPath + '/ckfinder/ckfinder.html?Type=Images'; 11 config.filebrowserFlashBrowseUrl = ckfinderPath + '/ckfinder/ckfinder.html?Type=Flash'; 12 config.filebrowserUploadUrl = ckfinderPath + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files'; 13 config.filebrowserImageUploadUrl = ckfinderPath + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images'; 14 config.filebrowserFlashUploadUrl = ckfinderPath + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash'; 15 };
使用測試,在插入超鏈接、插入圖片、插入文件中都有“上傳”l 因為上傳文件是非常危險的動作,因此在文件上傳的時候會進行權限校驗。在config.ascx的CheckAuthentication方法中校驗是否有權限上傳,返回true表示有權限,否則沒有權限,一般修改成判斷用戶是否登錄,並且登錄用戶是有上傳權限的用戶,可以用Session或者Membership來做。
1 public override bool CheckAuthentication() 2 { 3 // WARNING : DO NOT simply return "true". By doing so, you are allowing 4 // "anyone" to upload and list the files in your server. You must implement 5 // some kind of session validation here. Even something very simple as... 6 // 7 // return ( Session[ "IsAuthorized" ] != null && (bool)Session[ "IsAuthorized" ] == true ); 8 // 9 // ... where Session[ "IsAuthorized" ] is set to "true" as soon as the 10 // user logs on your system. 11 object obj = Session["已經登錄"] = true; 12 if (obj!=null&Convert.ToBoolean(obj)==true) 13 { 14 return true; 15 } 16 else 17 { 18 return false; 19 } 20 }
思考:如何實現只有指定IP地址的用戶才能上傳?
if (Request.UserHostAddress == "129.0.0.0.1") { return true; }
在SetConfig函數中設置上傳文件夾的位置BaseUrl、縮略圖的位置,每種類型數據的上傳路徑、允許上傳的文件類型AllowedExtensions等。