mvc4使用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 = ;
- Hashtable hash = new Hashtable();
- HttpPostedFileBase file = Request.Files["imgFile"];
- if (file == null)
- {
- hash = new Hashtable();
- hash["error"] = ;
- hash["message"] = "请选择文件";
- return Json(hash);
- }
- string dirPath = Server.MapPath(savePath);
- if (!Directory.Exists(dirPath))
- {
- hash = new Hashtable();
- hash["error"] = ;
- 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"] = ;
- hash["message"] = "上传文件大小超过限制";
- return Json(hash);
- }
- if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring().ToLower()) == -)
- {
- hash = new Hashtable();
- hash["error"] = ;
- 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"] = ;
- 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 = ; i < dirList.Length; i++)
- {
- DirectoryInfo dir = new DirectoryInfo(dirList[i]);
- Hashtable hash = new Hashtable();
- hash["is_dir"] = true;
- hash["has_file"] = (dir.GetFileSystemInfos().Length > );
- hash["filesize"] = ;
- 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 = ; 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().ToLower()) >= );
- hash["filetype"] = file.Extension.Substring();
- 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 ;
- }
- if (x == null)
- {
- return -;
- }
- if (y == null)
- {
- return ;
- }
- 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 ;
- }
- if (x == null)
- {
- return -;
- }
- if (y == null)
- {
- return ;
- }
- 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 ;
- }
- if (x == null)
- {
- return -;
- }
- if (y == null)
- {
- return ;
- }
- FileInfo xInfo = new FileInfo(x.ToString());
- FileInfo yInfo = new FileInfo(y.ToString());
- return xInfo.Extension.CompareTo(yInfo.Extension);
- }
- }
- }
3.最后在处理提交的Action上加 [ValidateInput(false)]特性,不然提交的报错:从客户端(“”)中检测到有潜在危险的 Request.Form 值
mvc4使用KindEditor文本编辑器的更多相关文章
- asp.net mvc4 使用KindEditor文本编辑器
最近做项目要用文本编辑器,编辑器好多种,这里介绍KindEditor在asp.net mvc4中的使用方法. 一.准备工作: 1.下载KindEditor.去官网:http://www.kindsof ...
- ASP.NET 配置KindEditor文本编辑器
ASP.NET 配置KindEditor文本编辑器 跟着这篇博客做了两个小时,只搞出了下面这么个东西. 时间浪费在了原博客里这样的一句话:将kindeditor/asp.net/bin/LitJSON ...
- ASP.NET配置KindEditor文本编辑器-图文实例
1.什么是KindEditor KindEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开发人员可以用 KindEditor 把传统的多行文本输入框(tex ...
- ASP.NET配置KindEditor文本编辑器
文本编辑器:CKEditor和CKFinder KindEditor 1.KindEditor KindEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开 ...
- 使用kindeditor文本编辑器
aspx中代码: <%@ Page Language="C#" ValidateRequest="false" AutoEventWireup=" ...
- KindEditor 文本编辑器
官网:http://kindeditor.net/docs/usage.html 目前支持ASP.ASP.NET.PHP.JSP.
- kindeditor文本编辑器乱码中乱码问题解决办法
这个问题我已经解决掉了,不是更改内容的编码格式,只要将lang/zh_CN.js 这个文件的编码转换成unicode即可 操作方法是 用记事本打开这个文件,另存为,然后更改文件的编码格式为unico ...
- 后台文本编辑器KindEditor介绍
后台文本编辑器KindEditor介绍 我们在自己的个人主页添加文章内容的时候,需要对文章内容进行修饰,此时就需要文本编辑器助阵了! 功能预览 KindEditor文本编辑器 KindEditor文本 ...
- kindeditor在线文本编辑器过滤HTML的方法
在使用kindeditor文本编辑器时遇到的问题,客户直接从Excel里粘贴文本内容到文本编辑器中(能不能再懒一些),然后不调整粘贴内容直接就保存(你敢不敢再懒一些)!对于这种很无语的行径,我只能对他 ...
随机推荐
- Children's Game UVA - 10905
看90,956这样的串,在比较完之前,就确定大小的,必定选大的放在前.而x=98,y=980;这样的,比较x+y和y+x的大小.如果x+y更小,y就放前. #include <iostream& ...
- android------DDMS files not found: tools\hprof-conv.exe
好久没有Eclipse了,使用一下就遇到坑,使用eclipse突然发生这个问题:DDMS files not found: ***\sdk\tools\hprof-conv.exe,无法连接模拟器 在 ...
- SPL之Iterator(迭代器)接口
前言:SPL是用于解决典型问题(standard problems)的一组接口与类的集合. <?php /** * Class MyIterator * 在 PHP 中,通常情况下遍历数组使用 ...
- activiti部署流程定义时出错:INSERT INTO ACT_GE_BYTEARRAY,修改数据库编码
activiti部署流程定义时出错 // 部署流程定义 Deployment deployment = deploymentBuilder.deploy(); 错误信息:(有乱码的...没留下截图.. ...
- python 常用代码
获取标签名 h1 class 是h1usersoup.find(name="h1", attrs={"class":"h1user"});获 ...
- iBeacon室内定位原理解析【转】
目前,技术发展持续火热,因着iBeacon的定位精度和造价都比较符合国内室内定位的市场需求,下面我们来聊一聊iBeacon室内定位原理. iBeacon定位原理 iBeacon是一项低耗能蓝牙技术,工 ...
- Hadoop--之RPC开发
Hadoop--之RPC开发 介绍: 百度百科: RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.R ...
- Miniconda安装scrapy教程
一.背景说明 前两天想重新研究下Scrapy,当时的环境是PyCharm社区版+Python 3.7.使用pip安装一直报错 “distutils.errors.DistutilsPlatformEr ...
- Apache+Tomcat+mod_jk配置教程
0.说明 首先我们要弄明白mod_jk的作用是反向代理,而其实使用httpd.conf中的<VirtualHost>标签就可以实现反向代理,为什么还要多搞个mod_jk那么麻烦做反向代理. ...
- 回声TCP服务器端/客户端
一.TCP服务端 1.TCP服务端的默认函数调用顺序 socket()创建套接字 bind()分配套接字地址 listen()等待请求连接状态 accept()允许连接 read()/write()数 ...