Ueditor配置及在項目中的使用


引言

上篇中簡單介紹了Ueditor的兩種定制方式,想了解的請戳這里:Ueditor的兩種定制方式。在項目中,Ueditor該怎么使用更方便呢?很容易讓人想到將ueditor放入用戶控件頁,可以拖到需要的地方。

Ueditor結構

Ueditor使用步驟

一,修改配置文件ueditor.config.js,配置Ueditor路徑

1  window.UEDITOR_HOME_URL = "/Ueditor/";//"相對於網站根目錄的相對路徑"也就是以斜杠開頭的形如"/myProject/ueditor/"這樣的路徑。
2     var URL = window.UEDITOR_HOME_URL || getUEBasePath();//獲取Ueditor的路徑
3     //測試用 如果不知道路徑是什么可以通過alert來測試
4     //alert("URL:" + URL);
5     //alert("window.UEDITOR_HOME_URL:" + window.UEDITOR_HOME_URL);
6     //alert("getUEBasePath():" + getUEBasePath());

上傳圖片的路徑配置。這里將上傳的文件放入網站根目錄Upload文件夾下,修改配置文件,將修正地址改為URL+“../”

 1 , imagePath: URL + "../"                     //圖片修正地址,引用了fixedImagePath,如有特殊需求,可自行配置
 2         //,imageFieldName:"upfile"                   //圖片數據的key,若此處修改,需要在后台對應文件修改對應參數
 3         //,compressSide:0                            //等比壓縮的基准,確定maxImageSideLength參數的參照對象。0為按照最長邊,1為按照寬度,2為按照高度
 4         //,maxImageSideLength:900                    //上傳圖片最大允許的邊長,超過會自動等比縮放,不縮放就設置一個比較大的值,更多設置在image.html中
 5         , savePath: ['Upload']//['UploadFile', 'upload2', 'upload3']      //圖片保存在服務器端的目錄, 默認為空, 此時在上傳圖片時會向服務器請求保存圖片的目錄列表,
 6         // 如果用戶不希望發送請求, 則可以在這里設置與服務器端能夠對應上的目錄名稱列表
 7         //比如: savePath: [ 'upload1', 'upload2' ]
 8 
 9         //塗鴉圖片配置區
10         , scrawlUrl: URL + "net/scrawlUp.ashx"           //塗鴉上傳地址
11         , scrawlPath: URL + "../"                            //圖片修正地址,同imagePath
12 
13         //附件上傳配置區
14         , fileUrl: URL + "net/fileUp.ashx"               //附件上傳提交地址
15         , filePath: URL + "../"                   //附件修正地址,同imagePath
16         //,fileFieldName:"upfile"                    //附件提交的表單名,若此處修改,需要在后台對應文件修改對應參數
17 
18         //遠程抓取配置區
19         //,catchRemoteImageEnable:true               //是否開啟遠程圖片抓取,默認開啟
20         , catcherUrl: URL + "net/getRemoteImage.ashx"   //處理遠程圖片抓取的地址
21         , catcherPath: URL + "../"                  //圖片修正地址,同imagePath
22         //,catchFieldName:"upfile"                   //提交到后台遠程圖片uri合集,若此處修改,需要在后台對應文件修改對應參數
23         //,separater:'ue_separate_ue'               //提交至后台的遠程圖片地址字符串分隔符
24         //,localDomain:[]                            //本地頂級域名,當開啟遠程圖片抓取時,除此之外的所有其它域名下的圖片都將被抓取到本地,默認不抓取127.0.0.1和localhost
25 
26         //圖片在線管理配置區
27         , imageManagerUrl: URL + "net/imageManager.ashx"       //圖片在線管理的處理地址
28         , imageManagerPath: URL + "../"                                    //圖片修正地址,同imagePath
29 
30         //屏幕截圖配置區
31         , snapscreenHost: location.hostname                                 //屏幕截圖的server端文件所在的網站地址或者ip,請不要加http://
32         , snapscreenServerUrl: URL + "net/imageUp.ashx" //屏幕截圖的server端保存程序,UEditor的范例代碼為“URL +"server/upload/net/snapImgUp.ashx"”
33         , snapscreenPath: URL + "../"
34         , snapscreenServerPort: location.port                                   //屏幕截圖的server端端口
35         //,snapscreenImgAlign: ''                                //截圖的圖片默認的排版方式
36 
37         //word轉存配置區
38         , wordImageUrl: URL + "net/imageUp.ashx"             //word轉存提交地址
39         , wordImagePath: URL + "../"                       //
40         //,wordImageFieldName:"upfile"                     //word轉存表單名若此處修改,需要在后台對應文件修改對應參數

同時修改Uploader.cs類文件,添加“~/”

1  pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
2  uploadpath = cxt.Server.MapPath("~/"+pathbase);//獲取文件上傳路徑 

Config.cs類

1 /// <summary>
2 /// Config 的摘要說明
3 /// </summary>
4 public class Config
5 {
6     public static string[] ImageSavePath = new string[] { "Upload" };
7 }

二,新建Ueditor.ascx用戶控件頁。
1.引入需要的js文件

1 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Ueditor.ascx.cs" Inherits="Wolfy.UeditorDemo.UC.Ueditor" %>
2 <script type="text/javascript" src="../Ueditor/ueditor.config.js"></script>
3 <script type="text/javascript" src="../Ueditor/ueditor.all.js"></script>
4 <script type="text/javascript" src="../Ueditor/lang/zh-cn/zh-cn.js"></script>

2.添加editor容器,並實例化uditor

 1 <link href="/Ueditor/themes/default/css/ueditor.css" rel="stylesheet" />
 2 <div id='myEditor' runat="server"></div>
 3 <script type="text/javascript" charset="utf-8">
 4     window.onload = function () {
 5         UE.ui.Editor({
 6             //這部分配置 在ueditor.config.js配置文件中,放在這里是希望可以在Ueditor.ascx.cs中可以動態設置。可以將動態配置的功能在這里設置
 7             wordCount: '<%=WordCount?"true":"false"%>'
 8             , maximumWords: '<%=MaximumWords%>'
 9             , wordCountMsg: '<%=WordCountMsg%>'
10             , wordOverFlowMsg: '<%=WordOverFlowMsg%>'
11             , initialContent: '<%=SetContents %>'
12         }).render("<%=myEditor.ClientID%>");
13     }
14 </script>

3,Ueditor.ascx.cs代碼,設置常用屬性,這樣就可以在引入用戶控件的頁面,動態的設置屬性了。

  1  public partial class Ueditor : System.Web.UI.UserControl
  2     {
  3         public Ueditor()
  4         {
  5             style = new System.Text.StringBuilder();
  6         }
  7         private string setContents;
  8         /// <summary>
  9         /// 給文本賦值 需html解碼
 10         /// </summary>
 11         public string SetContents
 12         {
 13             get
 14             { return setContents; }
 15             set
 16             {
 17                 setContents = Server.HtmlDecode(value);
 18             }
 19         }
 20         private string editorName;
 21         /// <summary>
 22         /// 返回文本編輯內容
 23         /// </summary>
 24         public string EditorName
 25         {
 26             get
 27             {
 28                 return editorName;
 29             }
 30             set
 31             {
 32                 editorName = value;
 33                 if (value == "")
 34                 {
 35                     //默認值
 36                     editorName = "editorValue";
 37                 }
 38             }
 39         }
 40         private bool isHide;
 41         ///<summary>
 42         ///控件是否隱藏
 43         ///</summary>
 44         public bool IsHide
 45         {
 46             get { return isHide; }
 47             set
 48             {
 49                 isHide = value;
 50 
 51                 if (isHide)
 52                 {
 53                     style.Append("display:none;");
 54                 }
 55                 else
 56                 {
 57                     style.Append("display:block;");
 58                 }
 59             }
 60         }
 61         /// <summary>
 62         /// 設置高度
 63         /// </summary>
 64         public string Height { get; set; }
 65         /// <summary>
 66         /// 設置寬度
 67         /// </summary>
 68         public string Width { get; set; }
 69         ///<summary>
 70         ///設置相對路徑
 71         ///</summary>
 72         public string EditorPath { get; set; }
 73         private bool wordCount = false;
 74         /// <summary>
 75         /// 是否開啟字數統計
 76         /// </summary>
 77         public bool WordCount
 78         {
 79             get { return wordCount; }
 80             set { wordCount = value; }
 81         }
 82 
 83         private int maximumWords = 100000;
 84         /// <summary>
 85         /// 允許的最大字符數
 86         /// </summary>
 87         public int MaximumWords
 88         {
 89             get { return maximumWords; }
 90             set { maximumWords = value; }
 91         }
 92         /// <summary>
 93         /// 字數統計提示
 94         /// </summary>
 95         public string WordCountMsg
 96         {
 97             get
 98             {
 99                 return "當前已輸入 {#count} 個字符,您還可以輸入{#leave} 個字符";
100             }
101         }
102 
103         /// <summary>
104         /// 超出字數限制提示
105         /// </summary>
106         public string WordOverFlowMsg
107         {
108             get
109             {
110                 return "<span style=\"color:red;\">你輸入的字符個數已經超出最大允許值,服務器可能會拒絕保存!</span>";
111             }
112         }
113         private System.Text.StringBuilder style = null;
114         protected void Page_Load(object sender, EventArgs e)
115         {
116             // EditorPath = Request.ApplicationPath;
117             if (string.IsNullOrEmpty(Height))
118             {
119                 Height = "100px";
120             }
121             if (string.IsNullOrEmpty(Width))
122             {
123                 Width = "100px";
124             }
125             style.Append("Width:" + Width + ";Height:" + Height + ";");
126             AddStyle(style);
127             //為富文本編輯器 添加name屬性 根據name得到 富文本內容 必須
128             myEditor.Attributes.Add("name", this.EditorName);
129 
130         }
131         /// <summary>
132         ///  為用戶控件 添加樣式
133         /// </summary>
134         /// <param name="strStyle"></param>
135         private void AddStyle(System.Text.StringBuilder strStyle)
136         {
137             if (string.IsNullOrEmpty(myEditor.Attributes["style"]))
138             {
139                 myEditor.Attributes.Add("style", style.ToString());
140             }
141             else
142             {
143                 myEditor.Attributes["style"] += style;
144             }
145         }
146     }

測試

將用戶控件拖入Test.aspx頁面。

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Wolfy.UeditorDemo.Test" %>
 2 
 3 <%@ Register Src="UC/Ueditor.ascx" TagName="Ueditor" TagPrefix="wolfy" %>
 4 
 5 <!DOCTYPE html>
 6 
 7 <html xmlns="http://www.w3.org/1999/xhtml">
 8 <head runat="server">
 9     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10     <title></title>
11 </head>
12 <body>
13     <form id="form1" runat="server">
14         <div>
15             <wolfy:Ueditor ID="MyUeditor" runat="server" Width="600px" IsHide="false" Height="300px" EditorName="myEditor" />
16         </div>
17     </form>
18 </body>
19 </html>

結果:

 上傳圖片

需要將Uploader.cs和Config.cs的屬性中生成操作改為“內容”,還應注意路徑問題。

 選擇圖片單擊確定,就可以將圖片加入編輯器

 測試獲取編輯器內容,文字及圖片等信息。這里必須在web.config配置文件中取消校驗,因為獲取到的內容可能含有標簽,如果添加,則會出現如下黃頁錯誤

在ueditor/net/web.config中將下面的信息,復制到網站的web.config中即可。

1     <httpRuntime requestValidationMode="2.0" maxRequestLength="102400 "/>
2         <pages validateRequest="false" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"></pages>
3         <globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-CN"/>

 測試:

1    protected void btnSave_Click(object sender, EventArgs e)
2         {
3             //獲取ueditor內容
4             string content = Server.HtmlEncode(Request.Form[MyUeditor.EditorName]);
5             Response.Write(Server.HtmlDecode(content));
6         }

結果

ueditor的賦初值

比如在寫隨筆的時候,編輯功能的實現,就是這種將數據庫中存的內容,重新填入編輯器,編輯后然后保存。這里,由於lz電腦重裝系統后,sql一直安裝不上就不再測試了。

已經將該實現封裝在用戶控件的cs中了,可以直接將得到的htmlencode字符串賦給SetContents屬性即可。

1 //可以通過下面的設置 實現編輯功能
2             MyUeditor.SetContents = "";

總結

Ueditor在使用的時候,路徑確實是很頭疼的問題,這里將其放入用戶控件頁,也是使用方便的一種途徑。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM