介绍KindEditor在asp.net mvc4中的使用方法

.NET MVC 中使用 kindeditor 中图片上传和查看功能 .NET MVC 中使用 kindeditor 中图片上传和查看功能 的正确使用方式 立即下载

 最近做项目要用文本编辑器,编辑器好多种,这里介绍KindEditor在asp.net mvc4中的使用方法。

  一、准备工作:

    1.下载KindEditor.去官网:http://www.kindsoft.net/

    2.解压,拷贝到项目中(这里面有些例子,可以先删除掉在拷贝到项目中。)

  二、使用步骤:

    1.view页面

  

复制代码

<script>
    var editor;
    KindEditor.ready(function (K) {
        editor = K.create('textarea[id="content"]', {//textarea
            allowFileManager: true,                  //是否允许文件上传           
            allowUpload: true,                                      
            fileManagerJson: '/KindEditor/ProcessRequest', //浏览文件方法 
            uploadJson: '/KindEditor/UploadImage'          //上传文件方法     
        });
    });
</script>

TextArea:
<div class="editor-field">
                    @Html.TextAreaFor(model => model.Content, new { id="content",style = "width:750px;height:400px" })
                    @Html.ValidationMessageFor(model => model.Content)
</div>

复制代码

    2.Controller中定义方法,就是js中 fileManagerJson和uploadJson的执行方法

复制代码

public class KindEditorController : Controller
    {
        [HttpPost]
        public ActionResult UploadImage()
        {
            string savePath = "/Content/UploadImages/";
            string saveUrl = "/Content/UploadImages/";
            string fileTypes = "gif,jpg,jpeg,png,bmp";
            int maxSize = 1000000;

            Hashtable hash = new Hashtable();

            HttpPostedFileBase file = Request.Files["imgFile"];
            if (file == null)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "请选择文件";
                return Json(hash);
            }

            string dirPath = Server.MapPath(savePath);
            if (!Directory.Exists(dirPath))
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传目录不存在";
                return Json(hash);
            }

            string fileName = file.FileName;
            string fileExt = Path.GetExtension(fileName).ToLower();

            ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));

            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传文件大小超过限制";
                return Json(hash);
            }

            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传文件扩展名是不允许的扩展名";
                return Json(hash);
            }

            string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
            string filePath = dirPath + newFileName;
            file.SaveAs(filePath);
            string fileUrl = saveUrl + newFileName;

            hash = new Hashtable();
            hash["error"] = 0;
            hash["url"] = fileUrl;

            return Json(hash, "text/html;charset=UTF-8"); ;

        }

        public ActionResult ProcessRequest()
        {
            //根目录路径,相对路径
            String rootPath = "/Content/UploadImages/";
            //根目录URL,可以指定绝对路径,
            String rootUrl = "/Content/UploadImages/";
            //图片扩展名
            String fileTypes = "gif,jpg,jpeg,png,bmp";

            String currentPath = "";
            String currentUrl = "";
            String currentDirPath = "";
            String moveupDirPath = "";

            //根据path参数,设置各路径和URL
            String path = Request.QueryString["path"];
            path = String.IsNullOrEmpty(path) ? "" : path;
            if (path == "")
            {
                currentPath = Server.MapPath(rootPath);
                currentUrl = rootUrl;
                currentDirPath = "";
                moveupDirPath = "";
            }
            else
            {
                currentPath = Server.MapPath(rootPath) + path;
                currentUrl = rootUrl + path;
                currentDirPath = path;
                moveupDirPath = Regex.Replace(currentDirPath, @"(.*?)[^\/]+\/$", "$1");
            }

            //排序形式,name or size or type
            String order = Request.QueryString["order"];
            order = String.IsNullOrEmpty(order) ? "" : order.ToLower();

            //不允许使用..移动到上一级目录
            if (Regex.IsMatch(path, @"\.\."))
            {
                Response.Write("Access is not allowed.");
                Response.End();
            }
            //最后一个字符不是/
            if (path != "" && !path.EndsWith("/"))
            {
                Response.Write("Parameter is not valid.");
                Response.End();
            }
            //目录不存在或不是目录
            if (!Directory.Exists(currentPath))
            {
                Response.Write("Directory does not exist.");
                Response.End();
            }

            //遍历目录取得文件信息
            string[] dirList = Directory.GetDirectories(currentPath);
            string[] fileList = Directory.GetFiles(currentPath);

            switch (order)
            {
                case "size":
                    Array.Sort(dirList, new NameSorter());
                    Array.Sort(fileList, new SizeSorter());
                    break;
                case "type":
                    Array.Sort(dirList, new NameSorter());
                    Array.Sort(fileList, new TypeSorter());
                    break;
                case "name":
                default:
                    Array.Sort(dirList, new NameSorter());
                    Array.Sort(fileList, new NameSorter());
                    break;
            }

            Hashtable result = new Hashtable();
            result["moveup_dir_path"] = moveupDirPath;
            result["current_dir_path"] = currentDirPath;
            result["current_url"] = currentUrl;
            result["total_count"] = dirList.Length + fileList.Length;
            List<Hashtable> dirFileList = new List<Hashtable>();
            result["file_list"] = dirFileList;
            for (int i = 0; i < dirList.Length; i++)
            {
                DirectoryInfo dir = new DirectoryInfo(dirList[i]);
                Hashtable hash = new Hashtable();
                hash["is_dir"] = true;
                hash["has_file"] = (dir.GetFileSystemInfos().Length > 0);
                hash["filesize"] = 0;
                hash["is_photo"] = false;
                hash["filetype"] = "";
                hash["filename"] = dir.Name;
                hash["datetime"] = dir.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                dirFileList.Add(hash);
            }
            for (int i = 0; i < fileList.Length; i++)
            {
                FileInfo file = new FileInfo(fileList[i]);
                Hashtable hash = new Hashtable();
                hash["is_dir"] = false;
                hash["has_file"] = false;
                hash["filesize"] = file.Length;
                hash["is_photo"] = (Array.IndexOf(fileTypes.Split(','), file.Extension.Substring(1).ToLower()) >= 0);
                hash["filetype"] = file.Extension.Substring(1);
                hash["filename"] = file.Name;
                hash["datetime"] = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                dirFileList.Add(hash);
            }
            //Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
            //context.Response.Write(JsonMapper.ToJson(result));
            //context.Response.End();
            return Json(result, "text/html;charset=UTF-8", JsonRequestBehavior.AllowGet);
        }

        public class NameSorter : IComparer
        {
            public int Compare(object x, object y)
            {
                if (x == null && y == null)
                {
                    return 0;
                }
                if (x == null)
                {
                    return -1;
                }
                if (y == null)
                {
                    return 1;
                }
                FileInfo xInfo = new FileInfo(x.ToString());
                FileInfo yInfo = new FileInfo(y.ToString());

                return xInfo.FullName.CompareTo(yInfo.FullName);
            }
        }

        public class SizeSorter : IComparer
        {
            public int Compare(object x, object y)
            {
                if (x == null && y == null)
                {
                    return 0;
                }
                if (x == null)
                {
                    return -1;
                }
                if (y == null)
                {
                    return 1;
                }
                FileInfo xInfo = new FileInfo(x.ToString());
                FileInfo yInfo = new FileInfo(y.ToString());

                return xInfo.Length.CompareTo(yInfo.Length);
            }
        }

        public class TypeSorter : IComparer
        {
            public int Compare(object x, object y)
            {
                if (x == null && y == null)
                {
                    return 0;
                }
                if (x == null)
                {
                    return -1;
                }
                if (y == null)
                {
                    return 1;
                }
                FileInfo xInfo = new FileInfo(x.ToString());
                FileInfo yInfo = new FileInfo(y.ToString());

                return xInfo.Extension.CompareTo(yInfo.Extension);
            }
        }

    }

复制代码

    3.最后在处理提交的Action上加 [ValidateInput(false)]特性,不然提交的报错:从客户端(“”)中检测到有潜在危险的 Request.Form 值

     参考:http://blog.163.com/very_apple/blog/static/277592362012111155310526/

Winform Html Editor 使用kindeditor组件实现winform Html 编辑器 立即下载

相关推荐

跟着Synopsys报告,一步步拆解车规芯片FMEDA计算(含公式与避坑点)

本文详细解析了车规芯片FMEDA计算的全流程,基于Synopsys报告拆解实战指南,涵盖基础失效率建模、安全机制效能评估及安全指标合成等关键步骤。通过公式解析、数据准备清单和避坑点分享,帮助工程师掌握ISO26262标准下的硬件量化分析技术,提升车载系统设计可靠性。

weixin_30460489的博客 195

关于ASP.NET用MVC开发的Kindeditor使用以及上传图片的个人经验

关于Kindeditor使用以及上传图片的个人经验小谈与最近的问题 Kindeditor是一个很不错的富文本框插件,引入的话会给网站开发大大减少很多工作,只是在使用的时候看官网的介绍总会有一些理解上的差别,在这里我希望通过我的经验来记录一下遇见的问题并帮助各位使用Kindeditor。我一直学习并使用的是ASP.NET,最近在做MVC的一个网站,也由于网上很多资料没有这方面的内容,所以希望可以帮助...

qq_32824141的博客 592

当Kmer遇到多倍体:复杂基因组Survey分析中的避坑指南

本文针对小麦、棉花等多倍体及高杂合复杂基因组的Survey分析,深入解析了Kmer分布图解读中的常见陷阱。文章重点探讨了如何超越理想二倍体模型,利用Smudgeplot等工具准确判断基因组倍性,并提供了从参数优化、污染判断到结果验证的全流程实战指南,帮助研究者规避错误,获得更准确的基因组大小、杂合度等关键信息。

yhn45678901的博客 1008

asp.net mvc4 使用KindEditor文本编辑器

     最近做项目要用文本编辑器,编辑器好多种,这里介绍KindEditor在asp.net mvc4中的使用方法。   一、准备工作:     1.下载KindEditor.去官网:http://www.kindsoft.net/     2.解压,拷贝到项目中(这里面有些例子,可以先删除掉在拷贝到项目中。)   二、使用步骤:     1.view页面    ...

a530504862的博客 213

微信小程序利用Echarts加载中国地图的步骤

微信小程序如何利用Echarts加载地图?可按照如下四个步骤进行。(阅读时间大概3分钟)。

成都都成 5275

Virbox Protector:全方位守护 Android 应用安全

在数字化浪潮中,Android 应用面临着诸多安全挑战,如破解、篡改、盗版、数据窃取和钓鱼欺诈等。为了应对这些复杂的安全问题,Virbox Protector 应运而生,为 Android 应用提供高强度的专业化安全保护,支持对 APK 和 AAB 文件的加固保护,并兼容 Google Play,助力应用顺利出海。

VBP_2025的博客 997

MVC中KindEditor的简单使用

第一:下载KindEditor 首先将下载好的最新的KindEditor放入到MVC项目中的Scripts文件夹中。 第二:添加HomeController publicclass HomeController : Controller { public ActionResult Index() { re

郭新宇 天道酬勤 1683

大数据之VIP(Virtual IP,虚拟IP)负载均衡

VIP(Virtual IP,虚拟IP)负载均衡是一种在计算机网络中常用的技术,用于将网络请求流量均匀地分散到多个服务器上,以提高系统的可扩展性、可靠性和性能。

赵玉的专栏 4124

ASP.NET MVC中使用kindeditor富文本框、文件上传

在网站后台,我们经常需要发布一些信息(包含文字、图片等等)供用户查阅,这时如果有一些编辑器的使用那么就得心应手了,其实有很多第三方插件实现了此功能, 我们只需要选择性的使用即可,那么今天要介绍一款富文本编辑器的第三方插件 kindeditor 1.首先下载该插件:官网已经挂了,不知怎么回事,去搜索找找 下载完,有如下目录结构: 因为我是在ASP.NET MVC项目中引用的,只需如下即可,把不需要的删掉: 然后添加到项目中: 首先在asp.net文件夹中找到Demo案例,看如何使用的: <%@ P

qq_34550459的博客 1778

灯塔问题:贪心与动态规划算法解析

区间覆盖问题是算法设计中的经典案例,常用于理解贪心算法与动态规划的核心差异。贪心算法通过局部最优选择逐步构建全局解,具有O(n log n)的高效时间复杂度,特别适合处理一维区间覆盖场景。动态规划则采用自底向上的填表法,虽然通用性更强但复杂度较高。在实际工程中,这类算法广泛应用于无线基站部署、监控设备布置等资源优化场景。通过分析灯塔问题的两种解法,可以深入掌握算法选择的关键因素,如问题规模、性能要求和实现复杂度。

weixin_28720573的博客 259

postgresql 数据库 timescaledb 函数time_bucket_gapfill()报错解决及更换 license

最近在工作中使用postgresql 和 timescaledb 的time_bucket_gapfill()函数 报错,让更换license,在网上找了一大圈,没有找到,最后解决了这个问题,这篇文章记录我的解决办法查看timescaledb.license参数命令:show timescaledb.license;修改命令: 结果不行,报错如下:lter system set timescaledb.license=‘timescale’;那只能换包了如果出现上面那种情况,那我们只能先从官网上下载带 '

yang_z_1的博客 3249

Asp.net MVC使用KindEditor4

最近转入Asp.net MVC开发,WebForm下惯用的FreeTextBox已然不能用了。于是想找个功能全、界面雅、免费的纯JS编辑器,KindEditor4正好满足本人的要求,包括图片、Flash、视频上传及空间管理而且配置也异常的简单。下面简单说一下在Asp.net MVC环境下基本配置方法。 Step 1. 下载KindEditor 首先到KindEditor官网下载(目前是4.

萌萌的It人 www.itmmd.com 6503

高效 DEM 拼接

通过以上步骤的系统实施,能够实现高效、准确的大型地理信息系统 DEM 数据拼接,满足复杂和大规模项目的应用需求。

llhllq2015的博客 1034

kindeditor-4.1.10 结合 Asp.Net MVC 添加图片功能

  KindEditor是一套开源的HTML可视化编辑器,现在我要结合Asp.Net MVC4 上传图片功能,做相应的配置和修改, 其实网上也有人写过类似的文章了,我写出来是以防以后使用的时候出现这样的问题,所以收集起来做参考之用, 如果有人遇到这样的问题,可以和我一起交流,qq号在我博客上已经贴出来了。 下面就开始贴出我的源代码: js: var keditor; var kop...

weixin_33877885的博客 182

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

KindEditor在asp.net mvc4中使用

1、 准备工作 KindEditor,是一个相对来说功能比较强的编辑器(因为上篇博文中的HTMLBox不能上传文件),它的下载地址:http://www.kindsoft.net/down.php 解压后的文件如下图: 2、 创建ASP.NET MVC 2.0 Razor项目,选择“空”项。 A.          同上篇博文第2步的A。 B.          添加KindEditor...

weixin_34060299的博客 101

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

关于Asp.Net Mvc3.0 使用KindEditor4.0 上传图片与文件

http://blog.csdn.net/fyxq14hao/article/details/7245502 今天我们的Asp.Net Mvc 3的项目中,把KindEditor3.9改为 KindEditor4.0 。修改了js文件的引用后,发现还是无法上传图片,最后发现时图片上传中的参数名修改了  从imageUploadJson 改为了uploadJson。 &lt;script typ...

weixin_34211761的博客 118

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

上一篇: ASP.NET配置KindEditor文本编辑器
下一篇: C# Chrome上传附件时,直接崩溃,原因竟然是因为搜狗输入法
卓炳坤
博客等级 码龄10年 33粉丝 39原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值