MVC 5でKinEditorを使用

34845 ワード

参照:http://www.cnblogs.com/weicong/archive/2012/03/31/2427608.html
最初のステップ
KindEditorのソースファイルをプロジェクトに追加し、langディレクトリ、plugisディレクトリ、themesディレクトリ、kindeditor-min.jsファイルのみで/Scripts/kindeditorディレクトリに配置することをお勧めします.
MVC5中使用KinEditor
ステップ2
jsでtextareaをkineditorにバインドします.

 
ステップ3
コントロールメソッドを記述するには、次の手順に従います.
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 = 2000000;



            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(1).ToLower()) == -1)

            {

                return ShowError("
" + ((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"] = 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: /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) == -1) { 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 > 0); hash["filesize"] = 0; 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(1).ToLower()) >= 0); hash["filetype"] = file.Extension.Substring(1); 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 0; } if (x == null) { return -1; } if (y == null) { return 1; } 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 0; } if (x == null) { return -1; } if (y == null) { return 1; } 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 0; } if (x == null) { return -1; } if (y == null) { return 1; } var xInfo = new FileInfo(x.ToString()); var yInfo = new FileInfo(y.ToString()); return String.CompareOrdinal(xInfo.Extension, yInfo.Extension); } } #endregion

View Code
完璧にやる