在上篇《使用ASP.NET MVC+Entity Framework快速搭建博客系统》中,已经基本上可以实现博客分类和博客文章的CURD。但是,文章编辑界面实在是……

好吧,咱得搞专业点。来个传说中的富文本编辑器,度娘了一下,发现 KISSY Editor 和 UEditor 貌似比较简单的样子,既然这样就用百度的 UEditor 吧,到这里下载最新的.NET版。

解压后,将默认目录名称改为 ueditor 然后复制到项目的 Content 目录中,大概就像下图中的样子

打开~/Views/Post/Create.cshtml 将代码替换为下面的代码

@model ShowPin.Web.Models.Post

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Post</h4>
        <hr />
        @Html.ValidationSummary(true)
        <div class="form-group">
            @Html.LabelFor(model => model.CategoryId, "CategoryId", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("CategoryId", ViewBag.CategoryId as IQueryable<SelectListItem>, String.Empty, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CategoryId)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Id, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Id, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Id)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Title, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Title)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Content, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(m => m.Content)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.CreateDate, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.CreateDate, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CreateDate)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.hits, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.hits, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.hits)
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval", "~/Content/ueditor/ueditor.config.js", "~/Content/ueditor/ueditor.all.js")
    <script type="text/javascript">
        var editorOption = {
            initialFrameWidth: ,
            initialFrameHeight:
        };
        var editor = new baidu.editor.ui.Editor(editorOption);
        editor.render('Content');
        $('form').submit(function () {
            $('#Content').val(editor.getContent());
        });
    </script>
}

其中,下面这点JS代码就是初始化 UEditor 的代码

<script type="text/javascript">
        var editorOption = {
            initialFrameWidth: ,
            initialFrameHeight:
        };
        var editor = new baidu.editor.ui.Editor(editorOption);
        editor.render('Content');
        $('form').submit(function () {
            $('#Content').val(editor.getContent());
        });
    </script>

在浏览器上输入URL:http://localhost:10223/Post/Create

提交后……(*^__^*) 嘻嘻……出错了:

从客户端(Content="<p style="text-align...")中检测到有潜在危险的 Request.Form 值。

修改相应的 Action 代码如下

[HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include="Id,Title,Content,CreateDate,hits,CategoryId")] Post post)
        {
            if (ModelState.IsValid)
            {
                db.Posts.Add(post);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Title", post.CategoryId);
            return View(post);
        }

最关键的就是:[ValidateInput(false)] 这句了,按下 Shift+F6 生成当前项目,然后再提交。

OK,UEditor 已经整合好了,下面就是本文的重点了

UEditor 上传图片

将 ~/Content/ueditor/net/imageUp.ashx 文件删掉,新建一个“一般处理程序”,命名为 imageUp.ashx 编写如下代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ShowPin.Web.Content.ueditor.net
{
    /// <summary>
    /// imageUp 的摘要说明
    /// </summary>
    public class imageUp : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (!String.IsNullOrEmpty(context.Request.QueryString["fetch"]))
            {
                context.Response.AddHeader("Content-Type", "text/javascript;charset=utf-8");
                context.Response.Write(String.Format("updateSavePath([{0}]);", String.Join(", ", Config.ImageSavePath.Select(x => "\"" + x + "\""))));
                return;
            }

            context.Response.ContentType = "text/plain";

            //上传配置
            ;           //文件大小限制,单位MB                             //文件大小限制,单位MB
            string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };         //文件允许格式

            //上传图片
            Hashtable info = new Hashtable();
            Uploader up = new Uploader();

            string path = up.getOtherInfo(context, "dir");
            if (String.IsNullOrEmpty(path))
            {
                path = Config.ImageSavePath[];
            }
            )
            {
                context.Response.Write("{ 'state' : '非法上传目录' }");
                return;
            }

            info = up.upFile(context, path + '/', filetype, size);                   //获取上传状态

            string title = up.getOtherInfo(context, "pictitle");                   //获取图片描述
            string oriName = up.getOtherInfo(context, "fileName");                //获取原始文件名

            HttpContext.Current.Response.Write("{'url':'" + info["url"] + "','title':'" + title + "','original':'" + oriName + "','state':'" + info["state"] + "'}");  //向浏览器返回数据json数据
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

其实就是上一般删除的那个文件的代码,只是……唉 才疏学浅,不知道怎么解释……然后可以看到,文件上传功能可以用了

默认是上传到这个路径,可以根据需要自行修改

嗯,今天就到这里吧

ASP.NET MVC 中使用 UEditor 富文本编辑器的更多相关文章

  1. ASP.NET MVC5 中百度ueditor富文本编辑器的使用

    随着网站信息发布内容越来越多,越来越重视美观,富文本编辑就是不可缺少的了,众多编辑器比较后我选了百度的ueditor富文本编辑器. 百度ueditor富文本编辑器分为两种一种是完全版的ueditor, ...

  2. WEB项目中使用UEditor(富文本编辑器)

    Ueditor富文本编辑器是在很多项目里经常用到的框架,是百度开发团队开发的一款很好用的富文本编辑器 下面就是我在一个系统里用到的,有了富文本编辑器,管理员使用起来不是很方便? 所以本博客介绍这个富文 ...

  3. 在MVC中应用百度富文本编辑器

    1.下载.NET版本的百度富文本编辑器,前往 下载.NET版本百度富文本框 2.解压下载的.zip压缩包,将utf8-.net文件夹名称改为:ueditor,复制到MVC根目录下面.结构如下: App ...

  4. 在网站中使用UEditor富文本编辑器

    UEditor是由百度WEB前端研发部开发的所见即所得的开源富文本编辑器,具有轻量.可定制.用户体验优秀等特点. 官网链接 进入到下载页面,选择相应的版本下载 这里我们使用ASP.NET开发,所以选择 ...

  5. Vue 中使用UEditor富文本编辑器-亲测可用-vue-ueditor-wrap

    其中UEditor中也存在不少错误,再引用过程中. 但是UEditor相对还是比较好用的一个富文本编辑器. vue-ueditor-wrap说明 Vue + UEditor + v-model 双向绑 ...

  6. 对于MVC中应用百度富文本编辑器问题的解决办法

    1.对于应用富文本编辑器post提交表单内容提示有危险的解决办法: [ValidateInput(false)] //文本编辑器的表单提交不用提示危险 [HttpPost] public Action ...

  7. vue2.0项目中使用Ueditor富文本编辑器示例

    最近在vue项目中需要使用富文本编辑器,于是将Ueditor集成进来,作为公共组件. 在线预览:https://suweiteng.github.io/vue2-management-platform ...

  8. 在Vue2.0中集成UEditor 富文本编辑器

    在vue的'项目中遇到了需要使用富文本编辑器的需求,在github上看了很多vue封装的editor插件,很多对图片上传和视频上传的支持并不是很好,最终还是决定使用UEditor. 这类的文章网上有很 ...

  9. vue2.0项目中使用Ueditor富文本编辑器应用中出现的问题

    1.如何设置config中的内容 readonly:true,//只读模式wordCount:false,//是否开启字数统计enableAutoSave: false,//自动保存功能 重点:ena ...

随机推荐

  1. linux系统学习笔记:无死角理解保存的设置用户ID,设置用户ID位,有效用户ID,实际用户ID

    一.基本概念 实际用户ID(RUID):用于标识一个系统中用户是谁,一般是在登录之后,就被唯一的确定,就是登录的用户的uid. 有效用户ID(EUID):用于系统决定用户对系统资源的权限,也就是说当用 ...

  2. js循环遍历

    //获取多个用户id 一般用在复选框 cust_id = 1,2,3,4; var str = cust_id.split(",");          for (var key ...

  3. Python即时网络爬虫:API说明

    API说明——下载gsExtractor内容提取器 1,接口名称 下载内容提取器 2,接口说明 如果您想编写一个网络爬虫程序,您会发现大部分时间耗费在调测网页内容提取规则上,不讲正则表达式的语法如何怪 ...

  4. nginx的conf文件的详细配置

    #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | in ...

  5. Nginx学习之一-第一个程序Hello World

    本例子实现了一个简单的hello world程序.运行效果: 虚拟机Ubuntu中: win7中chrome浏览器: 一.config文件编写 Nginx提供了一种简单的方式将第三方的模块编译到Ngi ...

  6. gdal读写图像分块处理(精华版)

    一.gdal进行数据操作在安装好gdal后,即可调用gdal库中的函数.(需要包含的头文件:gdal_priv.h)1.打开数据集使用gdal库进行数据(影像)操作的第一步就是打开一个数据集.对于“数 ...

  7. MEMS陀螺仪(gyroscope)的工作原理

    传统的陀螺仪主要是利用角动量守恒原理,因此它主要是一个不停转动的物体,它的转轴指向不随承载它的支架的旋转而变化. 但是MEMS陀螺仪(gyroscope)的工作原理不是这样的,因为要用微机械技术在硅片 ...

  8. Delphi HTML5 Canvas组件

    最近去sourceforge瞎转悠,突然发了一个组件,关于Delphi下Html5的canvas的组件,大概浏览了一下源码,竟然是纯粹的Pascal代码,也就说完全的Delphi代码.不敢独享,现在上 ...

  9. Linux查看物理CPU个数、核数、逻辑CPU个数 (转)

    # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cpuinfo| ...

  10. 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...