ASP.Net程式中調用FCKeditor的介面

來源:互聯網
上載者:User

在官方預設的版本中,FCKeditor是不支援asp.net的(沒有供asp.net調用的介面),不過有一個供asp調用的介面,我根據fckeditor.asp這個檔案用C#改寫了一個.Net的介面,大家有需要可以參考。

<script language="c#" runat="Server">
public class FCKeditor
{
 private string sBasePath;
 private string sInstanceName;
 private string sWidth;
 private string sHeight;
 private string sToolbarSet;
 private string sValue;

 private ArrayList oConfig;

 public FCKeditor()
 {
  sBasePath  = "/admin/FCKeditor/";
  sWidth   = "100%";
  sHeight   = "200";
  sToolbarSet  = "Default";
  sValue   = "";
  oConfig = new ArrayList();
 }

 public string BasePath
 {
  get{ return sBasePath; }
  set{ sBasePath = value; }
 }

 public string InstanceName
 {
  get{ return sInstanceName; }
  set{ sInstanceName = value; }
 }

 public string Width
 {
  get{ return sWidth; }
  set{ sWidth = value; }
 }

 public string Height
 {
  get{ return sHeight; }
  set{ sHeight = value; }
 }

 public string ToolbarSet
 {
  get{ return sToolbarSet; }
  set{ sToolbarSet = value; }
 }

 public string Value
 {
   get{ return sValue; }
  set{ sValue = value; }
 }

 public void Config(string configKey, string configValue)
 {
  oConfig.Add(new DictionaryEntry(configKey, configValue));
 }

 public void Create(string instanceName)
 {
  HttpContext.Current.Response.Write("<div>");
  if(IsCompatible())
  {
   string sFile="";
   string sLink="";
   if(HttpContext.Current.Request["fcksource"] == "true"){ sFile = "fckeditor.original.html";}
   else{ sFile = "fckeditor.html"; }

   sLink = sBasePath + "editor/" + sFile + "?InstanceName=" + instanceName;

   if(sToolbarSet != "") sLink = sLink + "&Toolbar="+sToolbarSet;

   HttpContext.Current.Response.Write("<input type='hidden' id='" + instanceName + "' name='" + instanceName + "' value='" + HttpContext.Current.Server.HtmlEncode( sValue ) + "' style='display:none' />");
  
   HttpContext.Current.Response.Write("<input type='hidden' id='"+instanceName+"___Config' value='"+GetConfigFieldString()+"' style='display:none' />");
  
   HttpContext.Current.Response.Write("<iframe id='"+instanceName+"___Frame' src='"+sLink+"' width='"+sWidth+"' height='"+sHeight+"' frameborder='no' scrolling='no'></iframe>");
    }
    else
    {
   string sWidthCSS="";
   string sHeightCSS="";
  
   if(sWidth.IndexOf('%')!=-1){ sWidthCSS = sWidth; }
   else{ sWidthCSS = sWidth+"px"; }

   if(sHeight.IndexOf('%')!=-1){ sHeightCSS = sHeight; }
   else{ sHeightCSS = sHeight+"px"; }

   HttpContext.Current.Response.Write("<textarea name='"+instanceName+"' rows='4' cols='40' style='width: "+sWidthCSS+"; height: "+sHeightCSS+"'>"+HttpContext.Current.Server.HtmlEncode(sValue)+"</textarea>");
   }
  HttpContext.Current.Response.Write("</div>");
  }

 private bool IsCompatible()
  {
  string sAgent = HttpContext.Current.Request.UserAgent;
  double iVersion;
  bool iIsCompatible = false;
  if( sAgent.IndexOf("MSIE") !=-1 &&
      sAgent.IndexOf("mac") ==-1 &&
  sAgent.IndexOf("Opera") == -1)
  {
   try{ iVersion = Convert.ToDouble(sAgent.Substring(sAgent.IndexOf("MSIE")+4,4).Trim()); }
   catch{ iVersion=0; }
   iIsCompatible = ( iVersion >= 5.5 );
  }
  else
  { iIsCompatible = false; }
    return iIsCompatible;
  }

 private string GetConfigFieldString()
 {
  string sParams="";
  bool bFirst = true;
  foreach(DictionaryEntry de in oConfig)
  {
   if(bFirst == false){ sParams = sParams+"&"; }
   else{ bFirst = false; }
   sParams = sParams+EncodeConfig( de.Key.ToString() )+ "=" +EncodeConfig( de.Value.ToString() );
   }
  return sParams;
  }
 
 private string EncodeConfig( string valueToEncode )
 {
  string sEncodeConfig;
  sEncodeConfig = valueToEncode.Replace("&", "%26" );
  sEncodeConfig = sEncodeConfig.Replace("=", "%3D" );
  sEncodeConfig = sEncodeConfig.Replace("'", "%22" );
  return sEncodeConfig;
 }
}
</script>

使用方法:

<%
string sBasePath = Request.ServerVariables["PATH_INFO"];
FCKeditor oFCKeditor = new FCKeditor();
oFCKeditor.BasePath = "./FCKeditor/";
oFCKeditor.Width = "100%";
oFCKeditor.Height = "400";
oFCKeditor.ToolbarSet = "Basic";
oFCKeditor.Create("body");
%>

此外,這個編輯器的asp.net用的檔案上傳和瀏覽程式也是沒有的,不過可以在設定檔裡把asp設定為預設指令碼語言就行了,一般支援asp.net的伺服器都支援asp的,不過asp.net的程式就不可能直接調用asp的類了,因此我編寫了這個介面。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.