MVC5中使用KinEditor
参考:http://www.cnblogs.com/weicong/archive/2012/03/31/2427608.html
第一步
将 KindEditor 的源文件添加到项目中,建议放到 /Scripts/kindeditor 目录中,其中只需要有 lang目录、plugis目录、themes目录和kindeditor-min.js文件即可。

第二步
通过js讲textarea绑定到kineditor。
<textarea id="HD_Content" rows="20" name="HD_Content"></textarea>
第三步
编写control方法即可:
fileManagerJson: '@Url.Action("FileManager", "KinEditor")', //浏览文件方法
uploadJson: '@Url.Action("Upload", "KinEditor")' //上传文件方法
//文件保存目录路径
const string SavePath = "/UploadFile/"; #region uploadJson
//
// GET: /KindEditor/Upload
public ActionResult Upload()
{
//文件保存目录URL
var saveUrl = SavePath; //定义允许上传的文件扩展名
var extTable = new Hashtable
{
{"image", "gif,jpg,jpeg,png,bmp"},
{"flash", "swf,flv"},
{"media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"},
{"file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"}
}; //最大文件大小
const int maxSize = ; var imgFile = Request.Files["imgFile"]; if (imgFile == null)
{
return ShowError("请选择文件。");
} var dirPath = Server.MapPath(SavePath);
if (!Directory.Exists(dirPath))
{
//return ShowError("上传目录不存在。" + dirPath);
Directory.CreateDirectory(dirPath);
} var dirName = Request.QueryString["dir"];
if (String.IsNullOrEmpty(dirName))
{
dirName = "image";
} if (!extTable.ContainsKey(dirName))
{
return ShowError("目录名不正确。");
} var fileName = imgFile.FileName;
var extension = Path.GetExtension(fileName);
if (extension == null)
{
return ShowError("extension == null");
} var fileExt = extension.ToLower(); if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
{
return ShowError("上传文件大小超过限制。");
} if (String.IsNullOrEmpty(fileExt) ||
Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring().ToLower()) == -)
{
return ShowError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
} //创建文件夹
dirPath += dirName + "/";
saveUrl += dirName + "/";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
var ymd = DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
dirPath += ymd + "/";
saveUrl += ymd + "/";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
} var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", System.Globalization.DateTimeFormatInfo.InvariantInfo) + fileExt;
var filePath = dirPath + newFileName; imgFile.SaveAs(filePath); var fileUrl = saveUrl + newFileName; var hash = new Hashtable();
hash["error"] = ;
hash["url"] = fileUrl; return Json(hash, "text/html;charset=UTF-8");
}
private JsonResult ShowError(string message)
{
var hash = new Hashtable();
hash["error"] = ;
hash["message"] = message; return Json(hash, "text/html;charset=UTF-8");
} #endregion #region fileManagerJson
//
// GET: /KindEditor/FileManager
public ActionResult FileManager()
{
//根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/
var rootUrl = SavePath; //图片扩展名
const string fileTypes = "gif,jpg,jpeg,png,bmp"; String currentPath;
String currentUrl;
String currentDirPath ;
String moveupDirPath ; var dirPath = Server.MapPath(SavePath);
var dirName = Request.QueryString["dir"];
if (!String.IsNullOrEmpty(dirName))
{
if (Array.IndexOf("image,flash,media,file".Split(','), dirName) == -)
{
return Content("Invalid Directory name.");
}
dirPath += dirName + "/";
rootUrl += dirName + "/";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
} //根据path参数,设置各路径和URL
var path = Request.QueryString["path"];
path = String.IsNullOrEmpty(path) ? "" : path;
if (path == "")
{
currentPath = dirPath;
currentUrl = rootUrl;
currentDirPath = "";
moveupDirPath = "";
}
else
{
currentPath = dirPath + 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, @"\.\."))
{
return Content("Access is not allowed.");
} //最后一个字符不是/
if (path != "" && !path.EndsWith("/"))
{
return Content("Parameter is not valid.");
}
//目录不存在或不是目录
if (!Directory.Exists(currentPath))
{
return Content("Directory does not exist.");
} //遍历目录取得文件信息
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;
default:
Array.Sort(dirList, new NameSorter());
Array.Sort(fileList, new NameSorter());
break;
} var result = new Hashtable();
result["moveup_dir_path"] = moveupDirPath;
result["current_dir_path"] = currentDirPath;
result["current_url"] = currentUrl;
result["total_count"] = dirList.Length + fileList.Length;
var dirFileList = new List<Hashtable>();
result["file_list"] = dirFileList;
foreach (var t in dirList)
{
var dir = new DirectoryInfo(t);
var 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);
}
foreach (var t in fileList)
{
var file = new FileInfo(t);
var 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);
} return Json(result, "text/html;charset=UTF-8", JsonRequestBehavior.AllowGet);
} private class NameSorter : IComparer
{
public int Compare(object x, object y)
{
if (x == null && y == null)
{
return ;
}
if (x == null)
{
return -;
}
if (y == null)
{
return ;
}
var xInfo = new FileInfo(x.ToString());
var yInfo = new FileInfo(y.ToString()); return String.CompareOrdinal(xInfo.FullName, yInfo.FullName);
}
} private class SizeSorter : IComparer
{
public int Compare(object x, object y)
{
if (x == null && y == null)
{
return ;
}
if (x == null)
{
return -;
}
if (y == null)
{
return ;
}
var xInfo = new FileInfo(x.ToString());
var yInfo = new FileInfo(y.ToString()); return xInfo.Length.CompareTo(yInfo.Length);
}
} private class TypeSorter : IComparer
{
public int Compare(object x, object y)
{
if (x == null && y == null)
{
return ;
}
if (x == null)
{
return -;
}
if (y == null)
{
return ;
}
var xInfo = new FileInfo(x.ToString());
var yInfo = new FileInfo(y.ToString()); return String.CompareOrdinal(xInfo.Extension, yInfo.Extension);
}
} #endregion
完美搞定
MVC5中使用KinEditor的更多相关文章
- MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子
MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子: 前端: 我们以前在前端定义了两个控件: <div class="row"> < ...
- 【读书笔记】Ninject 在MVC5中的使用
从MVC3中就开始接触Ninject这个IOC工具.也一直是MVC Framework系列书籍中推荐的IOC工具,当然还有优秀的Autofac等.性能和使用上面个有千秋.下面先看一下Ninject的使 ...
- MVC5中Model层开发数据注解
ASP.NET MVC5中Model层开发,使用的数据注解有三个作用: 数据映射(把Model层的类用EntityFramework映射成对应的表) 数据验证(在服务器端和客户端验证数据的有效性) 数 ...
- ASP.NET MVC5中的数据注解
ASP.NET MVC5中Model层开发,使用的数据注解有三个作用: 数据映射(把Model层的类用EntityFramework映射成对应的表) 数据验证(在服务器端和客户端验证数据的有效性) 数 ...
- 【转】MVC5中的区域(Areas)
MVC本身提倡的就是关注点分离.但是当项目本身的业务逻辑足够复杂,如果所有的业务逻辑都写个Controller文件夹下面的时候,你会看到非常庞大的各种命名的Controller,这个时候区域的作用就非 ...
- MVC5中使用SignalR2.0实现实时聊天室
原文 MVC5中使用SignalR2.0实现实时聊天室 有时候需要浏览器和服务端保持实时的通讯(比如在线聊天),SignalR的出现让这一切变得非常简单.它能够让服务端向客户端实时的推送消息.如果用户 ...
- 在MVC5中的使用Ninject
在MVC5中的使用 Ninject 从MVC3中就开始接触Ninject这个IOC工具.也一直是MVC Framework系列书籍中推荐的IOC工具,当然还有优秀的Autofac等.性能和使用上面个有 ...
- asp.net mvc5中使用缓存依赖SqlCacheDependency
缓存是用来提高应用性能,降低服务器压力.适用于数据不易变,数据易通用的情景, 对于动态查询数据,例如数据分析,最好放弃使用缓存.使用缓存最麻烦的就是保持源数据和缓存的中的数据一致. 缓存(Cache) ...
- 实例分析ASP.NET在MVC5中使用MiniProfiler监控MVC性能的方法
这篇文章主要为大家详细介绍了ASP.NET MVC5使用MiniProfiler监控MVC性能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 MiniProfiler ,一个简单而有效的迷你剖析器 ...
随机推荐
- java模拟OSUnMapTbl[]
问题描述: 任务就绪表,记录当前就绪的任务,就绪表中把64个优先级的任务分成8组,优先级的1-3bit表示OSRdyTbl[]中组别OSRedyGrp,优先级的4-6bit表示每组中就绪任务的位置,当 ...
- OSUnMapTbl[]的原理
问题描述: ucos任务队列中优先级获取 问题解决: uCOS-II是一个多任务的操作系统,每个任务都是一个应用程序,它有自己的寄存器和堆栈空间,即任务控制块TCB(task control ...
- easyui DateTimeBox 取值
$('#dt').datetimebox('getValue')
- HZNU1015: 矩阵排序
http://acm.hznu.edu.cn/JudgeOnline/problem.php?id=1015 题意:把矩阵每一行都排序. (以前觉得很难的题目回头看看原来如此简单 ][]; ; i&l ...
- httpclient 302 重定向
主要是由于页面可能不是通过 request.sendRedirect跳转的,可能通过js location跳转的.所以需要拿跳转后的 地址,重新发送请求...如下所示 if (status == H ...
- java基本对象Integer,String比较相等及equal案例说明
Integer i = new Integer(100); Integer i2 = new Integer(100); if(i == i2){ System.out.println("A ...
- Android 获取JSP或ASP的sessionId(Cookie)
如果使用的是httpClient: try { HttpResponse response = HttpUtil.httpClient.execute(new HttpGet(url)); Heade ...
- SQL四种语言:DDL,DML,DCL,TCL
1.DDL(Data Definition Language)数据库定义语言statements are used to define the database structure or schema ...
- codeforces #313 div1 D
好神的题目! 首先我们运用pick定理A=S-B/2+1将要求的东西转化掉 之后分离变量,我们变成了求选取凸包面积的期望和求选取凸包在边界上的点的期望 我们先考虑求选取凸包面积的期望 如何计算凸多边形 ...
- The only legal comparisons are between two numbers, two strings, or two dates.
The only legal comparisons are between two numbers, two strings, or two dates. Left hand operand is ...