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+“../”
, imagePath: URL + "../"                     //图片修正地址,引用了fixedImagePath,如有特殊需求,可自行配置
        //,imageFieldName:"upfile"                   //图片数据的key,若此处修改,需要在后台对应文件修改对应参数
        //,compressSide:0                            //等比压缩的基准,确定maxImageSideLength参数的参照对象。0为按照最长边,1为按照宽度,2为按照高度
        //,maxImageSideLength:900                    //上传图片最大允许的边长,超过会自动等比缩放,不缩放就设置一个比较大的值,更多设置在image.html中
        , savePath: ['Upload']//['UploadFile', 'upload2', 'upload3']      //图片保存在服务器端的目录, 默认为空, 此时在上传图片时会向服务器请求保存图片的目录列表,
        // 如果用户不希望发送请求, 则可以在这里设置与服务器端能够对应上的目录名称列表
        //比如: savePath: [ 'upload1', 'upload2' ]

        //涂鸦图片配置区
        , scrawlUrl: URL + "net/scrawlUp.ashx"           //涂鸦上传地址
        , scrawlPath: URL + "../"                            //图片修正地址,同imagePath

        //附件上传配置区
        , fileUrl: URL + "net/fileUp.ashx"               //附件上传提交地址
        , filePath: URL + "../"                   //附件修正地址,同imagePath
        //,fileFieldName:"upfile"                    //附件提交的表单名,若此处修改,需要在后台对应文件修改对应参数

        //远程抓取配置区
        //,catchRemoteImageEnable:true               //是否开启远程图片抓取,默认开启
        , catcherUrl: URL + "net/getRemoteImage.ashx"   //处理远程图片抓取的地址
        , catcherPath: URL + "../"                  //图片修正地址,同imagePath
        //,catchFieldName:"upfile"                   //提交到后台远程图片uri合集,若此处修改,需要在后台对应文件修改对应参数
        //,separater:'ue_separate_ue'               //提交至后台的远程图片地址字符串分隔符
        //,localDomain:[]                            //本地顶级域名,当开启远程图片抓取时,除此之外的所有其它域名下的图片都将被抓取到本地,默认不抓取127.0.0.1和localhost

        //图片在线管理配置区
        , imageManagerUrl: URL + "net/imageManager.ashx"       //图片在线管理的处理地址
        , imageManagerPath: URL + "../"                                    //图片修正地址,同imagePath

        //屏幕截图配置区
        , snapscreenHost: location.hostname                                 //屏幕截图的server端文件所在的网站地址或者ip,请不要加http://
        , snapscreenServerUrl: URL + "net/imageUp.ashx" //屏幕截图的server端保存程序,UEditor的范例代码为“URL +"server/upload/net/snapImgUp.ashx"”
        , snapscreenPath: URL + "../"
        , snapscreenServerPort: location.port                                   //屏幕截图的server端端口
        //,snapscreenImgAlign: ''                                //截图的图片默认的排版方式

        //word转存配置区
        , wordImageUrl: URL + "net/imageUp.ashx"             //word转存提交地址
        , wordImagePath: URL + "../"                       //
        //,wordImageFieldName:"upfile"                     //word转存表单名若此处修改,需要在后台对应文件修改对应参数
同时修改Uploader.cs类文件,添加“~/”
1  pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
2  uploadpath = cxt.Server.MapPath("~/"+pathbase);//获取文件上传路径 
Config.cs类

/// <summary>
/// Config 的摘要说明
/// </summary>
public class Config
{
    public static string[] ImageSavePath = new string[] { "Upload" };
}
二,新建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

<link href="/Ueditor/themes/default/css/ueditor.css" rel="stylesheet" />
<div id='myEditor' runat="server"></div>
<script type="text/javascript" charset="utf-8">
    window.onload = function () {
        UE.ui.Editor({
            //这部分配置 在ueditor.config.js配置文件中,放在这里是希望可以在Ueditor.ascx.cs中可以动态设置。可以将动态配置的功能在这里设置
            wordCount: '<%=WordCount?"true":"false"%>'
            , maximumWords: '<%=MaximumWords%>'
            , wordCountMsg: '<%=WordCountMsg%>'
            , wordOverFlowMsg: '<%=WordOverFlowMsg%>'
            , initialContent: '<%=SetContents %>'
        }).render("<%=myEditor.ClientID%>");
    }
</script>
3,Ueditor.ascx.cs代码,设置常用属性,这样就可以在引入用户控件的页面,动态的设置属性了。
public partial class Ueditor : System.Web.UI.UserControl
    {
        public Ueditor()
        {
            style = new System.Text.StringBuilder();
        }
        private string setContents;
        /// <summary>
        /// 给文本赋值 需html解码
        /// </summary>
        public string SetContents
        {
            get
            { return setContents; }
            set
            {
                setContents = Server.HtmlDecode(value);
            }
        }
        private string editorName;
        /// <summary>
        /// 返回文本编辑内容
        /// </summary>
        public string EditorName
        {
            get
            {
                return editorName;
            }
            set
            {
                editorName = value;
                if (value == "")
                {
                    //默认值
                    editorName = "editorValue";
                }
            }
        }
        private bool isHide;
        ///<summary>
        ///控件是否隐藏
        ///</summary>
        public bool IsHide
        {
            get { return isHide; }
            set
            {
                isHide = value;

                if (isHide)
                {
                    style.Append("display:none;");
                }
                else
                {
                    style.Append("display:block;");
                }
            }
        }
        /// <summary>
        /// 设置高度
        /// </summary>
        public string Height { get; set; }
        /// <summary>
        /// 设置宽度
        /// </summary>
        public string Width { get; set; }
        ///<summary>
        ///设置相对路径
        ///</summary>
        public string EditorPath { get; set; }
        private bool wordCount = false;
        /// <summary>
        /// 是否开启字数统计
        /// </summary>
        public bool WordCount
        {
            get { return wordCount; }
            set { wordCount = value; }
        }

        private int maximumWords = 100000;
        /// <summary>
        /// 允许的最大字符数
        /// </summary>
        public int MaximumWords
        {
            get { return maximumWords; }
            set { maximumWords = value; }
        }
        /// <summary>
        /// 字数统计提示
        /// </summary>
        public string WordCountMsg
        {
            get
            {
                return "当前已输入 {#count} 个字符,您还可以输入{#leave} 个字符";
            }
        }

        /// <summary>
        /// 超出字数限制提示
        /// </summary>
        public string WordOverFlowMsg
        {
            get
            {
                return "<span style=\"color:red;\">你输入的字符个数已经超出最大允许值,服务器可能会拒绝保存!</span>";
            }
        }
        private System.Text.StringBuilder style = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            // EditorPath = Request.ApplicationPath;
            if (string.IsNullOrEmpty(Height))
            {
                Height = "100px";
            }
            if (string.IsNullOrEmpty(Width))
            {
                Width = "100px";
            }
            style.Append("Width:" + Width + ";Height:" + Height + ";");
            AddStyle(style);
            //为富文本编辑器 添加name属性 根据name得到 富文本内容 必须
            myEditor.Attributes.Add("name", this.EditorName);

        }
        /// <summary>
        ///  为用户控件 添加样式
        /// </summary>
        /// <param name="strStyle"></param>
        private void AddStyle(System.Text.StringBuilder strStyle)
        {
            if (string.IsNullOrEmpty(myEditor.Attributes["style"]))
            {
                myEditor.Attributes.Add("style", style.ToString());
            }
            else
            {
                myEditor.Attributes["style"] += style;
            }
        }
    }

测试

将用户控件拖入Test.aspx页面。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Wolfy.UeditorDemo.Test" %>

<%@ Register Src="UC/Ueditor.ascx" TagName="Ueditor" TagPrefix="wolfy" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <wolfy:Ueditor ID="MyUeditor" runat="server" Width="600px" IsHide="false" Height="300px" EditorName="myEditor" />
        </div>
    </form>
</body>
</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"/>
测试:
 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在使用的时候,路径确实是很头疼的问题,这里将其放入用户控件页,也是使用方便的一种途径。








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
为了在Vue项目使用UEditor,你需要先安装UEditor的npm包。你可以使用以下命令进行安装: ``` npm install vue-ueditor-wrap --save ``` 安装完成后,你可以在Vue组件引入UEditor使用。下面是一个简单的示例: ```html <template> <div> <vue-ueditor-wrap :options="editorConfig" v-model="content"></vue-ueditor-wrap> </div> </template> <script> import VueUeditorWrap from 'vue-ueditor-wrap' export default { components: { VueUeditorWrap }, data() { return { content: '', editorConfig: { UEDITOR_HOME_URL: '/static/UEditor/', // UEditor资源文件的根目录 serverUrl: '/api/upload', // UEditor上传图片和文件的后端接口地址 toolbars: [ [ 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'strikethrough', 'removeformat', '|', 'insertorderedlist', 'insertunorderedlist', '|', 'link', 'unlink', 'anchor', '|', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'fullscreen' ] ], } } } } </script> ``` 这个示例,我们首先在组件引入了`vue-ueditor-wrap`组件。然后我们在`data`定义了一个`content`变量来保存UEditor编辑器的内容,并定义了一个`editorConfig`对象来配置UEditor。 最后,我们在模板使用`vue-ueditor-wrap`组件,将它的`options`属性设置为我们定义的`editorConfig`对象,并使用`v-model`指令将编辑器的内容绑定到`content`变量上。 当用户在UEditor编辑内容时,`content`变量的值也会相应地更新。你可以在组件使用`content`变量来获取编辑器的内容,以便将其保存到后端或进行其他操作。 希望这个示例对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值