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

  一、准备工作:

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

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

  二、使用步骤:

1.view页面

  1. <script>
  2. var editor;
  3. KindEditor.ready(function (K) {
  4. editor = K.create('textarea[id="content"]', {//textarea
  5. allowFileManager: true, //是否允许文件上传
  6. allowUpload: true,
  7. fileManagerJson: '/KindEditor/ProcessRequest', //浏览文件方法
  8. uploadJson: '/KindEditor/UploadImage' //上传文件方法
  9. });
  10. });
  11. </script>
  12.  
  13. TextArea:
  14. <div class="editor-field">
  15. @Html.TextAreaFor(model => model.Content, new { id="content",style = "width:750px;height:400px" })
  16. @Html.ValidationMessageFor(model => model.Content)
  17. </div>

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

  1. public class KindEditorController : Controller
  2. {
  3. //上传方法
  4. [HttpPost]
  5. public ActionResult UploadImage()
  6. {
  7. string savePath = "/Content/UploadImages/";
  8. string saveUrl = "/Content/UploadImages/";
  9. string fileTypes = "gif,jpg,jpeg,png,bmp";
  10. int maxSize = ;
  11.  
  12. Hashtable hash = new Hashtable();
  13.  
  14. HttpPostedFileBase file = Request.Files["imgFile"];
  15. if (file == null)
  16. {
  17. hash = new Hashtable();
  18. hash["error"] = ;
  19. hash["message"] = "请选择文件";
  20. return Json(hash);
  21. }
  22.  
  23. string dirPath = Server.MapPath(savePath);
  24. if (!Directory.Exists(dirPath))
  25. {
  26. hash = new Hashtable();
  27. hash["error"] = ;
  28. hash["message"] = "上传目录不存在";
  29. return Json(hash);
  30. }
  31.  
  32. string fileName = file.FileName;
  33. string fileExt = Path.GetExtension(fileName).ToLower();
  34.  
  35. ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));
  36.  
  37. if (file.InputStream == null || file.InputStream.Length > maxSize)
  38. {
  39. hash = new Hashtable();
  40. hash["error"] = ;
  41. hash["message"] = "上传文件大小超过限制";
  42. return Json(hash);
  43. }
  44.  
  45. if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring().ToLower()) == -)
  46. {
  47. hash = new Hashtable();
  48. hash["error"] = ;
  49. hash["message"] = "上传文件扩展名是不允许的扩展名";
  50. return Json(hash);
  51. }
  52.  
  53. string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
  54. string filePath = dirPath + newFileName;
  55. file.SaveAs(filePath);
  56. string fileUrl = saveUrl + newFileName;
  57.  
  58. hash = new Hashtable();
  59. hash["error"] = ;
  60. hash["url"] = fileUrl;
  61.  
  62. return Json(hash, "text/html;charset=UTF-8"); ;
  63.  
  64. }
  65.  
  66. //浏览方法
  67. public ActionResult ProcessRequest()
  68. {
  69. //根目录路径,相对路径
  70. String rootPath = "/Content/UploadImages/";
  71. //根目录URL,可以指定绝对路径,
  72. String rootUrl = "/Content/UploadImages/";
  73. //图片扩展名
  74. String fileTypes = "gif,jpg,jpeg,png,bmp";
  75.  
  76. String currentPath = "";
  77. String currentUrl = "";
  78. String currentDirPath = "";
  79. String moveupDirPath = "";
  80.  
  81. //根据path参数,设置各路径和URL
  82. String path = Request.QueryString["path"];
  83. path = String.IsNullOrEmpty(path) ? "" : path;
  84. if (path == "")
  85. {
  86. currentPath = Server.MapPath(rootPath);
  87. currentUrl = rootUrl;
  88. currentDirPath = "";
  89. moveupDirPath = "";
  90. }
  91. else
  92. {
  93. currentPath = Server.MapPath(rootPath) + path;
  94. currentUrl = rootUrl + path;
  95. currentDirPath = path;
  96. moveupDirPath = Regex.Replace(currentDirPath, @"(.*?)[^\/]+\/$", "$1");
  97. }
  98.  
  99. //排序形式,name or size or type
  100. String order = Request.QueryString["order"];
  101. order = String.IsNullOrEmpty(order) ? "" : order.ToLower();
  102.  
  103. //不允许使用..移动到上一级目录
  104. if (Regex.IsMatch(path, @"\.\."))
  105. {
  106. Response.Write("Access is not allowed.");
  107. Response.End();
  108. }
  109. //最后一个字符不是/
  110. if (path != "" && !path.EndsWith("/"))
  111. {
  112. Response.Write("Parameter is not valid.");
  113. Response.End();
  114. }
  115. //目录不存在或不是目录
  116. if (!Directory.Exists(currentPath))
  117. {
  118. Response.Write("Directory does not exist.");
  119. Response.End();
  120. }
  121.  
  122. //遍历目录取得文件信息
  123. string[] dirList = Directory.GetDirectories(currentPath);
  124. string[] fileList = Directory.GetFiles(currentPath);
  125.  
  126. switch (order)
  127. {
  128. case "size":
  129. Array.Sort(dirList, new NameSorter());
  130. Array.Sort(fileList, new SizeSorter());
  131. break;
  132. case "type":
  133. Array.Sort(dirList, new NameSorter());
  134. Array.Sort(fileList, new TypeSorter());
  135. break;
  136. case "name":
  137. default:
  138. Array.Sort(dirList, new NameSorter());
  139. Array.Sort(fileList, new NameSorter());
  140. break;
  141. }
  142.  
  143. Hashtable result = new Hashtable();
  144. result["moveup_dir_path"] = moveupDirPath;
  145. result["current_dir_path"] = currentDirPath;
  146. result["current_url"] = currentUrl;
  147. result["total_count"] = dirList.Length + fileList.Length;
  148. List<Hashtable> dirFileList = new List<Hashtable>();
  149. result["file_list"] = dirFileList;
  150. for (int i = ; i < dirList.Length; i++)
  151. {
  152. DirectoryInfo dir = new DirectoryInfo(dirList[i]);
  153. Hashtable hash = new Hashtable();
  154. hash["is_dir"] = true;
  155. hash["has_file"] = (dir.GetFileSystemInfos().Length > );
  156. hash["filesize"] = ;
  157. hash["is_photo"] = false;
  158. hash["filetype"] = "";
  159. hash["filename"] = dir.Name;
  160. hash["datetime"] = dir.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  161. dirFileList.Add(hash);
  162. }
  163. for (int i = ; i < fileList.Length; i++)
  164. {
  165. FileInfo file = new FileInfo(fileList[i]);
  166. Hashtable hash = new Hashtable();
  167. hash["is_dir"] = false;
  168. hash["has_file"] = false;
  169. hash["filesize"] = file.Length;
  170. hash["is_photo"] = (Array.IndexOf(fileTypes.Split(','), file.Extension.Substring().ToLower()) >= );
  171. hash["filetype"] = file.Extension.Substring();
  172. hash["filename"] = file.Name;
  173. hash["datetime"] = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  174. dirFileList.Add(hash);
  175. }
  176. //Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
  177. //context.Response.Write(JsonMapper.ToJson(result));
  178. //context.Response.End();
  179. return Json(result, "text/html;charset=UTF-8", JsonRequestBehavior.AllowGet);
  180. }
  181.  
  182. public class NameSorter : IComparer
  183. {
  184. public int Compare(object x, object y)
  185. {
  186. if (x == null && y == null)
  187. {
  188. return ;
  189. }
  190. if (x == null)
  191. {
  192. return -;
  193. }
  194. if (y == null)
  195. {
  196. return ;
  197. }
  198. FileInfo xInfo = new FileInfo(x.ToString());
  199. FileInfo yInfo = new FileInfo(y.ToString());
  200.  
  201. return xInfo.FullName.CompareTo(yInfo.FullName);
  202. }
  203. }
  204.  
  205. public class SizeSorter : IComparer
  206. {
  207. public int Compare(object x, object y)
  208. {
  209. if (x == null && y == null)
  210. {
  211. return ;
  212. }
  213. if (x == null)
  214. {
  215. return -;
  216. }
  217. if (y == null)
  218. {
  219. return ;
  220. }
  221. FileInfo xInfo = new FileInfo(x.ToString());
  222. FileInfo yInfo = new FileInfo(y.ToString());
  223.  
  224. return xInfo.Length.CompareTo(yInfo.Length);
  225. }
  226. }
  227.  
  228. public class TypeSorter : IComparer
  229. {
  230. public int Compare(object x, object y)
  231. {
  232. if (x == null && y == null)
  233. {
  234. return ;
  235. }
  236. if (x == null)
  237. {
  238. return -;
  239. }
  240. if (y == null)
  241. {
  242. return ;
  243. }
  244. FileInfo xInfo = new FileInfo(x.ToString());
  245. FileInfo yInfo = new FileInfo(y.ToString());
  246.  
  247. return xInfo.Extension.CompareTo(yInfo.Extension);
  248. }
  249. }
  250.  
  251. }

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

mvc4使用KindEditor文本编辑器的更多相关文章

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

    最近做项目要用文本编辑器,编辑器好多种,这里介绍KindEditor在asp.net mvc4中的使用方法. 一.准备工作: 1.下载KindEditor.去官网:http://www.kindsof ...

  2. ASP.NET 配置KindEditor文本编辑器

    ASP.NET 配置KindEditor文本编辑器 跟着这篇博客做了两个小时,只搞出了下面这么个东西. 时间浪费在了原博客里这样的一句话:将kindeditor/asp.net/bin/LitJSON ...

  3. ASP.NET配置KindEditor文本编辑器-图文实例

    1.什么是KindEditor KindEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开发人员可以用 KindEditor 把传统的多行文本输入框(tex ...

  4. ASP.NET配置KindEditor文本编辑器

    文本编辑器:CKEditor和CKFinder  KindEditor 1.KindEditor KindEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开 ...

  5. 使用kindeditor文本编辑器

    aspx中代码: <%@ Page Language="C#" ValidateRequest="false" AutoEventWireup=" ...

  6. KindEditor 文本编辑器

    官网:http://kindeditor.net/docs/usage.html 目前支持ASP.ASP.NET.PHP.JSP.

  7. kindeditor文本编辑器乱码中乱码问题解决办法

    这个问题我已经解决掉了,不是更改内容的编码格式,只要将lang/zh_CN.js  这个文件的编码转换成unicode即可 操作方法是 用记事本打开这个文件,另存为,然后更改文件的编码格式为unico ...

  8. 后台文本编辑器KindEditor介绍

    后台文本编辑器KindEditor介绍 我们在自己的个人主页添加文章内容的时候,需要对文章内容进行修饰,此时就需要文本编辑器助阵了! 功能预览 KindEditor文本编辑器 KindEditor文本 ...

  9. kindeditor在线文本编辑器过滤HTML的方法

    在使用kindeditor文本编辑器时遇到的问题,客户直接从Excel里粘贴文本内容到文本编辑器中(能不能再懒一些),然后不调整粘贴内容直接就保存(你敢不敢再懒一些)!对于这种很无语的行径,我只能对他 ...

随机推荐

  1. Children's Game UVA - 10905

    看90,956这样的串,在比较完之前,就确定大小的,必定选大的放在前.而x=98,y=980;这样的,比较x+y和y+x的大小.如果x+y更小,y就放前. #include <iostream& ...

  2. android------DDMS files not found: tools\hprof-conv.exe

    好久没有Eclipse了,使用一下就遇到坑,使用eclipse突然发生这个问题:DDMS files not found: ***\sdk\tools\hprof-conv.exe,无法连接模拟器 在 ...

  3. SPL之Iterator(迭代器)接口

    前言:SPL是用于解决典型问题(standard problems)的一组接口与类的集合. <?php /** * Class MyIterator * 在 PHP 中,通常情况下遍历数组使用 ...

  4. activiti部署流程定义时出错:INSERT INTO ACT_GE_BYTEARRAY,修改数据库编码

    activiti部署流程定义时出错 // 部署流程定义 Deployment deployment = deploymentBuilder.deploy(); 错误信息:(有乱码的...没留下截图.. ...

  5. python 常用代码

    获取标签名 h1 class 是h1usersoup.find(name="h1", attrs={"class":"h1user"});获 ...

  6. iBeacon室内定位原理解析【转】

    目前,技术发展持续火热,因着iBeacon的定位精度和造价都比较符合国内室内定位的市场需求,下面我们来聊一聊iBeacon室内定位原理. iBeacon定位原理 iBeacon是一项低耗能蓝牙技术,工 ...

  7. Hadoop--之RPC开发

    Hadoop--之RPC开发   介绍: 百度百科: RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.R ...

  8. Miniconda安装scrapy教程

    一.背景说明 前两天想重新研究下Scrapy,当时的环境是PyCharm社区版+Python 3.7.使用pip安装一直报错 “distutils.errors.DistutilsPlatformEr ...

  9. Apache+Tomcat+mod_jk配置教程

    0.说明 首先我们要弄明白mod_jk的作用是反向代理,而其实使用httpd.conf中的<VirtualHost>标签就可以实现反向代理,为什么还要多搞个mod_jk那么麻烦做反向代理. ...

  10. 回声TCP服务器端/客户端

    一.TCP服务端 1.TCP服务端的默认函数调用顺序 socket()创建套接字 bind()分配套接字地址 listen()等待请求连接状态 accept()允许连接 read()/write()数 ...