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里粘贴文本内容到文本编辑器中(能不能再懒一些),然后不调整粘贴内容直接就保存(你敢不敢再懒一些)!对于这种很无语的行径,我只能对他 ...
随机推荐
- MyEclipse6.5的反编译插件的安装
常用的几种反编译工具 1. JD-GUI[推荐] JD-GUI是属于Java Decompiler项目(JD项目)下个的图形化运行方式的反编译器.JD-Eclipse属于Java Decompiler ...
- vue select的change事件,将点击过的城市名存在数组中,下次调用不需要再调用接口
<template> <div id="body" class="application" v-show="show" v ...
- 2017.6.5项目总结(移动端touch事件)
event.stopPropagation() 该方法将停止事件的传播,阻止它被分派到其他Document节点.在时间传播的任何阶段都可以调用它,注意,虽然该方法不能阻止同一个Document节点上 ...
- 机器学习 之k-means和DBSCAN的区别
目录 1.定义和区别(优缺点对比) 2.kmeans原理 3.DBSCAN原理 1.定义和区别(优缺点对比) 聚类分为:基于划分.层次.密度.图形和模型五大类: 均值聚类k-means是基于划分的聚类 ...
- DRF之权限认证,过滤分页,异常处理
1. 认证Authentication 在配置文件中配置全局默认的认证方案 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_f ...
- Tree Requests CodeForces - 570D (dfs水题)
大意: 给定树, 每个节点有一个字母, 每次询问子树$x$内, 所有深度为$h$的结点是否能重排后构成回文. 直接暴力对每个高度建一棵线段树, 查询的时候相当于求子树内异或和, 复杂度$O((n+m) ...
- 有用的git命令
1. git log -p // 查看log的详细信息 2. git reset HEAD xxxx // 将文件从stage状态拉出来 3. git checkout -- xxxx // 将修改的 ...
- Leetcode 5016. 删除最外层的括号
5016. 删除最外层的括号 显示英文描述 我的提交返回竞赛 用户通过次数446 用户尝试次数469 通过次数456 提交次数577 题目难度Easy 有效括号字符串为空 ("&quo ...
- Hadoop 2.7.3 完全分布式维护-部署篇
测试环境如下 IP host JDK linux hadop role 172.16.101.55 sht-sgmhadoopnn-01 1.8.0_111 CentOS release ...
- Oracle 11.2.0.4.0 Dataguard部署和日常维护(1)-数据库安装篇
本次测试环境 系统版本 CentOS release 6.8 主机名 ec2t-userdata-01 ec2t-userdata-01 IP地址 10.189.102.118 10.189.100. ...