2023年7月3日发(作者:)
在 MVC 5 中应用 KindEditor
第一步
将 KindEditor 的源文件添加到工程中,建议放到 /Scripts/kindeditor 目录中,其中只需要有 lang目录、plugis目录、themes目录和文件即可。
第二步
在 /Views/Shared/EditorTemplates 目录中添加一个分部视图“〞(文件名可任意)。代码如下:
@("~/Scripts/kindeditor/")
@ea(, tedModelValue)
学习文档 仅供参考 第三步
在需要应用编辑器的Model属性中设置 DataAnnotations,比方:
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
[AllowHtml]
[UIHint("kindeditor")] // EditorTemplates 目录中添加的视图名称
public String Content { get; set; }
}
第四步
在视图中使用 @For(model => t) 即可加载编辑器。
附 KindEditorHandlerController 源码
using System;
using tions;
using c;
using ization;
using ;
using rExpressions;
using ;
namespace llers
{
public class KindEditorHandlerController : Controller
{
学习文档 仅供参考 //文件保存目录路径
const string SavePath = "/uploadfile/";
#region uploadJson
//
// GET: /KindEditorHandler/Upload
public ActionResult Upload()
{
////文件保存目录路径
//const string savePath = "/Content/Uploads/";
//文件保存目录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 = 2000000;
var imgFile = ["imgFile"];
学习文档 仅供参考 if (imgFile == null)
{
return ShowError("请选择文件。");
}
var dirPath = h(SavePath);
if (!(dirPath))
{
//return ShowError("上传目录不存在。" + dirPath);
Directory(dirPath);
}
var dirName = tring["dir"];
if (OrEmpty(dirName))
{
dirName = "image";
}
if (!nsKey(dirName))
{
return ShowError("目录名不正确。");
}
var fileName = me;
var extension = ension(fileName);
if (extension == null)
{
学习文档 仅供参考 return ShowError("extension == null");
}
var fileExt = r();
if (tream == null || > maxSize)
{
return ShowError("上传文件大小超过限制。");
}
if (OrEmpty(fileExt) ||
f(((String)extTable[dirName]).Split(','), ing(1).ToLower()) == -1)
{
return ShowError("上传文件扩展名是不允许的扩展名。n只允许" +
((String)extTable[dirName]) + "格式。");
}
//创立文件夹
dirPath += dirName + "/";
saveUrl += dirName + "/";
if (!(dirPath))
{
Directory(dirPath);
}
var ymd = ng("yyyyMMdd", antInfo);
dirPath += ymd + "/";
saveUrl += ymd + "/";
学习文档 仅供参考 if (!(dirPath))
{
Directory(dirPath);
}
var newFileName = ng("yyyyMMddHHmmss_ffff",
antInfo) + fileExt;
var filePath = dirPath + newFileName;
(filePath);
var fileUrl = saveUrl + newFileName;
var hash = new Hashtable();
hash["error"] = 0;
hash["url"] = fileUrl;
return Json(hash, "text/html;charset=UTF-8"); }
private JsonResult ShowError(string message)
{
var hash = new Hashtable();
hash["error"] = 1;
hash["message"] = message;
return Json(hash, "text/html;charset=UTF-8"); }
学习文档 仅供参考
#endregion
#region fileManagerJson
//
// GET: /KindEditorHandler/FileManager
public ActionResult FileManager()
{
////根目录路径,相对路径
//String rootPath = "/Content/Uploads/";
//根目录URL,可以指定绝对路径,比方 :// yoursite /attached/
var rootUrl = SavePath;
//图片扩展名
const string fileTypes = "gif,jpg,jpeg,png,bmp";
String currentPath;
String currentUrl;
String currentDirPath;
String moveupDirPath;
var dirPath = h(SavePath);
var dirName = tring["dir"];
if (!OrEmpty(dirName))
{
if (f("image,flash,media,file".Split(','), dirName) == -1)
{
学习文档 仅供参考 return Content("Invalid Directory name.");
}
dirPath += dirName + "/";
rootUrl += dirName + "/";
if (!(dirPath))
{
Directory(dirPath);
}
}
//根据path参数,设置各路径和URL
var path = tring["path"];
path = OrEmpty(path) ? "" : path;
if (path == "")
{
currentPath = dirPath;
currentUrl = rootUrl;
currentDirPath = "";
moveupDirPath = "";
}
else
{
currentPath = dirPath + path;
currentUrl = rootUrl + path;
currentDirPath = path;
moveupDirPath = e(currentDirPath, @"(.*?)[^]+$", "$1");
学习文档 仅供参考 }
//排序形式,name or size or type
String order = tring["order"];
order = OrEmpty(order) ? "" : r();
//不允许使用..移动到上一级目录
if (h(path, @".."))
{
return Content("Access is not allowed.");
}
//最后一个字符不是/
if (path != "" && !th("/"))
{
return Content("Parameter is not valid.");
}
//目录不存在或不是目录
if (!(currentPath))
{
return Content("Directory does not exist.");
}
//遍历目录取得文件信息
string[] dirList = ectories(currentPath);
string[] fileList = es(currentPath);
switch (order)
学习文档 仅供参考 {
case "size":
(dirList, new NameSorter());
(fileList, new SizeSorter());
break;
case "type":
(dirList, new NameSorter());
(fileList, new TypeSorter());
break;
default:
(dirList, new NameSorter());
(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"] = + ;
var dirFileList = new List
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"] = (eSystemInfos().Length > 0);
hash["filesize"] = 0;
hash["is_photo"] = false;
hash["filetype"] = "";
hash["filename"] = ;
hash["datetime"] = ng("yyyy-MM-dd HH:mm:ss");
(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"] = ;
hash["is_photo"] = (f((','),
ing(1).ToLower()) >= 0);
hash["filetype"] = ing(1);
hash["filename"] = ;
hash["datetime"] = ng("yyyy-MM-dd HH:mm:ss");
(hash);
}
return Json(result, "text/html;charset=UTF-8", et);
学习文档 仅供参考 }
private class NameSorter : IComparer
{
public int Compare(object x, object y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
var xInfo = new FileInfo(ng());
var yInfo = new FileInfo(ng());
return String pareOrdinal(me, me);
}
}
private class SizeSorter : IComparer
学习文档 仅供参考 {
public int Compare(object x, object y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
var xInfo = new FileInfo(ng());
var yInfo = new FileInfo(ng());
return pareTo();
}
}
private class TypeSorter : IComparer
{
public int Compare(object x, object y)
{
学习文档 仅供参考 if (x == null && y == null)
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
var xInfo = new FileInfo(ng());
var yInfo = new FileInfo(ng());
return String pareOrdinal(ion, ion);
}
}
#endregion
}
}
学习文档 仅供参考
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688380745a129386.html
评论列表(0条)