【要什么自行车】ASP.NET MVC4笔记03:调用编辑器 kindeditor
参考:http://www.cnblogs.com/guzhongx/p/kindeditor.html
1、下载kindeditor,存放于Content文件夹下
<script src="~/Content/kindeditor/kindeditor-all.js"></script>
2、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 id="content" style="width:750px;height:400px"></textarea>
3、新建一个名为KindEditor的Controller
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc; namespace MvcStudy.Controllers
{
public class KindEditorController : Controller
{ [HttpPost]
public ActionResult UploadImage()
{
string savePath = "/files/upload/images/";
string saveUrl = "/files/upload/images/";
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 = "/files/upload/images/";
//根目录URL,可以指定绝对路径,
String rootUrl = "/files/upload/images/";
//图片扩展名
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);
}
} }
}
【要什么自行车】ASP.NET MVC4笔记03:调用编辑器 kindeditor的更多相关文章
- asp.net mvc4使用百度ueditor编辑器
原文 http://www.cnblogs.com/flykai/p/3285307.html 已测试 相当不错 前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好 ...
- [转载] ASP.NET MVC4使用百度UEDITOR编辑器
前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好几个问题,以此来记录解决办法.编辑器可以到http://ueditor.baidu.com/website/downloa ...
- 【要什么自行车】ASP.NET MVC4笔记02:上传文件 uploadify 组件使用
参考:http://www.cnblogs.com/luotaoyeah/p/3321070.html 1.下载 uploadify 组件,copy至 Content文件夹 <link href ...
- 【要什么自行车】ASP.NET MVC4笔记01:Asp.net MVC 分页,采用 MvcPager 和CYQ.Data来分页
Control: ) { ; ; using (MAction action = new MAction("brain")) { MDataTable table = action ...
- Asp .Net MVC4笔记之走进MVC
一.MVC三层架构: mvc三层架构,大家都比较熟悉了,这里再介绍一下.Mvc将应用程序分离为三个部分: Model:是一组类,用来描述被处理的数据,同时也定义这些数据如何被变更和操作的业务规则.与数 ...
- Asp .Net MVC4笔记之目录结构
认识MVC从目录结构开始,从基本创建开始. App_Data 文件夹:App_Data 文件夹用于存储应用程序数据. App_Start:启动文件的配置信息,包括很重要的RouteConfig路由注册 ...
- Asp.net MVC4高级编程学习笔记-视图学习第一课20171009
首先解释下:本文只是对Asp.net MVC4高级编程这本书学习记录的学习笔记,书本内容感觉挺简单的,但学习容易忘记,因此在边看的同时边作下了笔记,可能其它朋友看的话没有情境和逻辑顺序还请谅解! 一. ...
- C#面试题(转载) SQL Server 数据库基础笔记分享(下) SQL Server 数据库基础笔记分享(上) Asp.Net MVC4中的全局过滤器 C#语法——泛型的多种应用
C#面试题(转载) 原文地址:100道C#面试题(.net开发人员必备) https://blog.csdn.net/u013519551/article/details/51220841 1. . ...
- 读《asp.net MVC4开发指南(黄保翕编著)》笔记
在刚刚过去的中秋节中,利用了两天的碎片时间把黄保翕编著的<asp.net MVC4 开发指南>看了遍,笔记如下,欢饮在开发MVC的同学一起来探讨: 1.社区 2.开源程序 3.易测试性 4 ...
随机推荐
- ComponentOne 2016 年产品规划
作为老牌的 Visual Studio 控件集,ComponentOne 今后的发展方向是什么?新的一年会在哪些方面有所增强?且听 ComponentOne 全球产品经理的 2016年规划. 2016 ...
- 10 个 Redis 建议/技巧
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/145.html?1455790611 Redis 在当前的技术社区里是非常 ...
- 2013 duilib入门简明教程 -- 总结 (20)
duilib的入门系列就到尾声了,再次提醒下,Alberl用的duilib版本是SVN上第个版本,时间是2013.08.15~ 这里给出Alberl最后汇总的一个工程,戳我下载,效 ...
- Atitit 编程语言知识点tech tree v2 attilax大总结
Atitit 编程语言知识点tech tree v2 attilax大总结 大分类中分类小分类知识点原理与规范具体实现(javac#里面的实现phpjsdsl(自己实现其他语言实现 类与对象实现对象实 ...
- 多线程 ThreadPool线程池
简单说明一下: 线程池可以看做容纳线程的容器:一个应用程序最多只能有一个线程池:ThreadPool静态类通过QueueUserWorkItem()方法将工作函数排入线程池: 每排入一个工作函数,就相 ...
- 利用Nodejs快速构建应用原型
利用Nodejs快速构建应用原型 开发一个应用往往需要快速的构建原型,然后在此基础上设计和改进,前端可能立马能看到效果,但是后端业务逻辑不会那么快,这个时候其实我们需要额只是一些模拟数据,所以不需要真 ...
- linux配置hosts
linux配置hosts linux下配置hosts和windows下其实就是一样的,找到文件在哪里就好 sudo vim /etc/hosts
- SSM环境搭建(接口编程方式)
一直用ssm在开发项目,之前都是直接copy别人的项目,今天趁着项目刚刚交付,自己搭建一下ssm环境,做个记录 一.创建项目.引入jar包,因为版本不一样,就不贴出这部分的内容了.个人平时的习惯是,先 ...
- Xdebug 配置
到官网 http://www.xdebug.com/download.php 下载 找到对应PHP版本的 Xdebug ,后面带 TS 的为线程安全,本机环境为 win7 64 + php-5.5.1 ...
- IO流-文本IO\读写二进制数据
文本IO 一.简述 OutputStreamWriter类使用选定的编码方式吧Unicode字符流转换为字节流,InputStreamReader类将包含字节的输入流转为可以产生Unicode字符的读 ...