ASP.NET筆記之CKEditor的使用方法
配置參考文檔,主要將ckeditor中的(adapters、images、lang、plugins、skins、themes、ckeditor.js、config.js、contents.css)解壓到j(luò)s目錄,然后“顯示所有文件”,將ckeditor的目錄“包含在項目中”,在發(fā)帖頁面引用ckeditor.js,然后設(shè)置多行文本框的class="ckeditor"(CSS強大)(服務(wù)端控件CssClass=" ckeditor ",客戶端控件要設(shè)定cols、rows屬性,一般不直接用html控件),代碼中仍然可以通過TextBox控件的Text屬性來訪問編輯器內(nèi)容。
由于頁面提交的時候asp.net會把富文本編輯器中的html內(nèi)容當(dāng)成攻擊內(nèi)容,因此需要在aspx中的Page標(biāo)簽中設(shè)置 ValidateRequest="false" 來禁用攻擊檢測(2010中還要根據(jù)報錯信息修改WebConfig來禁用XSS檢測)。
遇到錯誤如下:
**修改WebConfig來禁用XSS檢測
當(dāng)asp.net提交“<>”這些字符到aspx頁面時,如果沒有在文件頭中加入“ValidateRequest="false"”這句話,就會出現(xiàn)出錯提示:從客戶端(<?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,將上傳的處理程序設(shè)定為CKFinder,注意路徑的問題。
CKEDITOR.editorConfig = function( config )
{
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
//改成ckfinder的絕對路徑,從網(wǎng)站的本目錄開始
var ckfinderPath = "/admin/js";
config.filebrowserBrowseUrl = ckfinderPath + '/ckfinder/ckfinder.html';
config.filebrowserImageBrowseUrl = ckfinderPath + '/ckfinder/ckfinder.html?Type=Images';
config.filebrowserFlashBrowseUrl = ckfinderPath + '/ckfinder/ckfinder.html?Type=Flash';
config.filebrowserUploadUrl = ckfinderPath + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files';
config.filebrowserImageUploadUrl = ckfinderPath + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images';
config.filebrowserFlashUploadUrl = ckfinderPath + '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash';
};
使用測試,在插入超鏈接、插入圖片、插入文件中都有“上傳”l 因為上傳文件是非常危險的動作,因此在文件上傳的時候會進行權(quán)限校驗。在config.ascx的CheckAuthentication方法中校驗是否有權(quán)限上傳,返回true表示有權(quán)限,否則沒有權(quán)限,一般修改成判斷用戶是否登錄,并且登錄用戶是有上傳權(quán)限的用戶,可以用Session或者Membership來做。
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.
object obj = Session["已經(jīng)登錄"] = true;
if (obj!=null&Convert.ToBoolean(obj)==true)
{
return true;
}
else
{
return false;
}
}
思考:如何實現(xiàn)只有指定IP地址的用戶才能上傳?
if (Request.UserHostAddress == "129.0.0.0.1") { return true; }
在SetConfig函數(shù)中設(shè)置上傳文件夾的位置BaseUrl、縮略圖的位置,每種類型數(shù)據(jù)的上傳路徑、允許上傳的文件類型AllowedExtensions等。
相關(guān)文章
ASP.NET SignaiR 實現(xiàn)消息的即時推送,并使用Push.js實現(xiàn)通知的示例代碼
ASP.NET SignalR 是為 ASP.NET 開發(fā)人員提供的一個庫,可以簡化開發(fā)人員將實時 Web 功能添加到應(yīng)用程序的過程。有興趣的可以了解一下。2017-01-01.net c# gif動畫如何添加圖片水印實現(xiàn)思路及代碼
本文將詳細(xì)介紹下c#實現(xiàn)gif動畫添加圖片水印,思路很清晰,感興趣的你可以參考下哈,希望可以幫助到你2013-03-03ASP.NET生成eurl.axd Http異常錯誤的處理方法
在IIS6中同時啟用了ASP.NET 2.0 和 ASP.NET 4.0 后,網(wǎng)站程序可能會出現(xiàn)如下錯誤:“ System.Web.HttpException: Path ‘//eurl.axd/‘ was not found. ”2011-05-05WEB在模態(tài)窗體里導(dǎo)出或下載文件功能代碼
實現(xiàn)在模態(tài)窗體里導(dǎo)出或下載文件,具體功能代碼如下,感興趣的朋友可以參考下哈2013-06-06