ASP-NET-MVC-5-应用-KindEditor--UIHint_第1页
ASP-NET-MVC-5-应用-KindEditor--UIHint_第2页
ASP-NET-MVC-5-应用-KindEditor--UIHint_第3页
ASP-NET-MVC-5-应用-KindEditor--UIHint_第4页
ASP-NET-MVC-5-应用-KindEditor--UIHint_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、在 ASP.NET MVC 5 中应用 KindEditor第一步将 KindEditor 的源文件添加到工程中,建议放到 /Scripts/kindeditor 目录中,其中只需要有 lang目录、plugis目录、themes目录和kindeditor-min.js文件即可。第二步在 /Views/Shared/EditorTemplates 目录中添加一个分部视图“kindeditor.cshtml(文件名可任意)。代码如下:Scripts.Render("/Scripts/kindeditor/kindeditor-min.js")Html.TextArea(str

2、ing.Empty, ViewData.TemplateInfo.FormattedModelValue)<script type="text/javascript">/ <!CDATA    (function ()         KindEditor.ready(function (k)             k.create("#

3、ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)",                 themeType: 'default',                width: '690px',&#

4、160;               height: '400px',                uploadJson: '/KindEditorHandler/Upload',        

5、        fileManagerJson: '/KindEditorHandler/FileManager',                allowFileManager : true            );  

6、0;     );    )();/ ></script>第三步在需要应用编辑器的Model属性中设置 DataAnnotations,比方:    public class Movie            public int ID get; set;         public string Title

7、 get; set;         public DateTime ReleaseDate get; set;         public string Genre get; set;         public decimal Price get; set;         AllowHtml 

8、60;      UIHint("kindeditor") / EditorTemplates 目录中添加的视图名称        public String Content get; set;     第四步在视图中使用 Html.EditorFor(model => model.Content) 即可加载编辑器。附 KindEditorHandlerController 源码using System;using Sy

9、stem.Collections;using System.Collections.Generic;using System.Globalization;using System.IO;using System.Text.RegularExpressions;using System.Web.Mvc;namespace KwDoctorCourse.Controllers    public class KindEditorHandlerController : Controller       

10、0;    /文件保存目录路径        const string SavePath = "/uploadfile/"        #region uploadJson        /        / GET: /KindEditorHandler/Uploa

11、d        public ActionResult Upload()                    /文件保存目录路径            /const string savePath = "/Content/Up

12、loads/"            /文件保存目录URL            var saveUrl = SavePath;            /定义允许上传的文件扩展名       

13、;     var extTable = new Hashtable                                          

14、;                        "image", "gif,jpg,jpeg,png,bmp",                  

15、                 "flash", "swf,flv",                           &#

16、160;       "media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb",                              

17、     "file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"                               ;  &#

18、160;         /最大文件大小            const int maxSize = 2000000;             var imgFile = Request.Files"imgFile"    &#

19、160;        if (imgFile = null)                            return ShowError("请选择文件。");      

20、60;                var dirPath = Server.MapPath(SavePath);            if (!Directory.Exists(dirPath)             &#

21、160;              /return ShowError("上传目录不存在。" + dirPath);                Directory.CreateDirectory(dirPath);       

22、;                 var dirName = Request.QueryString"dir"            if (String.IsNullOrEmpty(dirName)          

23、;                  dirName = "image"                        if (!extTable.ContainsKey(dirName) 

24、                           return ShowError("目录名不正确。");                   &

25、#160;    var fileName = imgFile.FileName;            var extension = Path.GetExtension(fileName);            if (extension = null)       

26、60;                    return ShowError("extension = null");                        var fi

27、leExt = extension.ToLower();            if (imgFile.InputStream = null | imgFile.InputStream.Length > maxSize)                       

28、;     return ShowError("上传文件大小超过限制。");                        if (String.IsNullOrEmpty(fileExt) |          

29、60;     Array.IndexOf(String)extTabledirName).Split(','), fileExt.Substring(1).ToLower() = -1)                            return ShowError(&qu

30、ot;上传文件扩展名是不允许的扩展名。n只允许" + (String)extTabledirName) + "格式。");                        /创立文件夹            dirPath += dirNam

31、e + "/"            saveUrl += dirName + "/"            if (!Directory.Exists(dirPath)               

32、             Directory.CreateDirectory(dirPath);                        var ymd = DateTime.Now.ToString("yyyyMMdd", Date

33、TimeFormatInfo.InvariantInfo);            dirPath += ymd + "/"            saveUrl += ymd + "/"            if (!Dire

34、ctory.Exists(dirPath)                            Directory.CreateDirectory(dirPath);               &

35、#160;        var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;            var filePath = dirPath + newFileName;     &

36、#160;      imgFile.SaveAs(filePath);            var fileUrl = saveUrl + newFileName;            var hash = new Hashtable();       

37、     hash"error" = 0;            hash"url" = fileUrl;            return Json(hash, "text/html;charset=UTF-8");     

38、           private JsonResult ShowError(string message)                    var hash = new Hashtable();          

39、60; hash"error" = 1;            hash"message" = message;            return Json(hash, "text/html;charset=UTF-8");        

40、0;       #endregion        #region fileManagerJson        /        / GET: /KindEditorHandler/FileManager        public ActionResul

41、t FileManager()                    /根目录路径,相对路径            /String rootPath = "/Content/Uploads/"        &

42、#160;   /根目录URL,可以指定绝对路径,比方 :/ yoursite /attached/            var rootUrl = SavePath;            /图片扩展名            const stri

43、ng fileTypes = "gif,jpg,jpeg,png,bmp"            String currentPath;            String currentUrl;            String currentD

44、irPath;            String moveupDirPath;            var dirPath = Server.MapPath(SavePath);            var dirName = Request.QueryStrin

45、g"dir"            if (!String.IsNullOrEmpty(dirName)                            if (Array.IndexOf("i

46、mage,flash,media,file".Split(','), dirName) = -1)                                    return Content("Invalid Dir

47、ectory name.");                                dirPath += dirName + "/"          

48、0;     rootUrl += dirName + "/"                if (!Directory.Exists(dirPath)                    

49、60;               Directory.CreateDirectory(dirPath);                               &

50、#160;        /根据path参数,设置各路径和URL            var path = Request.QueryString"path"            path = String.IsNullOrEmpty(path) ? "" : pa

51、th;            if (path = "")                            currentPath = dirPath;    &#

52、160;           currentUrl = rootUrl;                currentDirPath = ""                mov

53、eupDirPath = ""                        else                       

54、0;    currentPath = dirPath + path;                currentUrl = rootUrl + path;                currentDirPath = path;   

55、             moveupDirPath = Regex.Replace(currentDirPath, "(.*?)/+/$", "$1");                        /排序形式,na

56、me or size or type            String order = Request.QueryString"order"            order = String.IsNullOrEmpty(order) ? "" : order.ToLower();    

57、0;       /不允许使用.移动到上一级目录            if (Regex.IsMatch(path, ".")                        &#

58、160;   return Content("Access is not allowed.");                        /最后一个字符不是/            if (path != "&qu

59、ot; && !path.EndsWith("/")                            return Content("Parameter is not valid.");       

60、0;                /目录不存在或不是目录            if (!Directory.Exists(currentPath)                 

61、60;          return Content("Directory does not exist.");                        /遍历目录取得文件信息       &

62、#160;    string dirList = Directory.GetDirectories(currentPath);            string fileList = Directory.GetFiles(currentPath);            switch (order)    

63、;                        case "size":                    Array.Sort(dirList, new Name

64、Sorter();                    Array.Sort(fileList, new SizeSorter();                    break;   

65、;             case "type":                    Array.Sort(dirList, new NameSorter();        

66、0;           Array.Sort(fileList, new TypeSorter();                    break;             

67、   default:                    Array.Sort(dirList, new NameSorter();                    Array.Sort(f

68、ileList, new NameSorter();                    break;                        var result = new Ha

69、shtable();            result"moveup_dir_path" = moveupDirPath;            result"current_dir_path" = currentDirPath;         

70、   result"current_url" = currentUrl;            result"total_count" = dirList.Length + fileList.Length;            var dirFileList = new List<Hashtable>()

71、;            result"file_list" = dirFileList;            foreach (var t in dirList)                 

72、           var dir = new DirectoryInfo(t);                var hash = new Hashtable();               

73、 hash"is_dir" = true;                hash"has_file" = (dir.GetFileSystemInfos().Length > 0);                hash"filesize&

74、quot; = 0;                hash"is_photo" = false;                hash"filetype" = ""      

75、          hash"filename" = dir.Name;                hash"datetime" = dir.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");     &

76、#160;          dirFileList.Add(hash);                        foreach (var t in fileList)         

77、60;                  var file = new FileInfo(t);                var hash = new Hashtable();         

78、       hash"is_dir" = false;                hash"has_file" = false;                hash"filesiz

79、e" = file.Length;                hash"is_photo" = (Array.IndexOf(fileTypes.Split(','), file.Extension.Substring(1).ToLower() >= 0);           &#

80、160;    hash"filetype" = file.Extension.Substring(1);                hash"filename" = file.Name;                has

81、h"datetime" = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");                dirFileList.Add(hash);                  

82、;      return Json(result, "text/html;charset=UTF-8", JsonRequestBehavior.AllowGet);                private class NameSorter : IComparer          

83、0;         public int Compare(object x, object y)                            if (x = null && y = null)   &#

84、160;                                return 0;                  &#

85、160;             if (x = null)                                    retu

86、rn -1;                                if (y = null)                 &#

87、160;                  return 1;                                va

88、r xInfo = new FileInfo(x.ToString();                var yInfo = new FileInfo(y.ToString();                return String pareOrdinal(xInfo.FullName, yI

89、nfo.FullName);                            private class SizeSorter : IComparer                 

90、   public int Compare(object x, object y)                            if (x = null && y = null)         

91、0;                          return 0;                        

92、0;       if (x = null)                                    return -1;             &#

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论