加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

KindEditor编辑器在ASP.NET中的使用

发布时间:2020-12-16 09:00:52 所属栏目:asp.Net 来源:网络整理
导读:KindEditor编辑器在ASP.NET中的使用 最近做的项目中都有用到富文本编辑器,一直在寻找最后用的富文本编辑器,之前用过CKEditor,也用过UEditor,这次打算用 一下KindEditor。 KindEditor简介 : KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网

KindEditor编辑器在ASP.NET中的使用

最近做的项目中都有用到富文本编辑器,一直在寻找最后用的富文本编辑器,之前用过CKEditor,也用过UEditor,这次打算用 一下KindEditor。

KindEditor简介

KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE、Firefox、Chrome、Safari、Opera等主流浏览器。KindEditor使用JavaScript编写,可以无缝的于Java、.NET、PHP、ASP等程序接合。 KindEditor非常适合在CMS、商城、论坛、博客、Wiki、电子邮件等互联网应用上使用,2006年7月首次发布2.0以来,KindEditor依靠出色的用户体验和领先的技术不断扩大编辑器市场占有率,目前在国内已经成为最受欢迎的编辑器之一。[来自百度百科]

官网简介、 官网下载 、官方文档 、官网Demo演示

KindEditor使用

  1. 将开发包导入到项目

    将下载的开发包中不需要的删掉,只保留项目需要的文件,我的项目是ASP.NET项目,所引用的开发包资源如下图所示

  2. 在页面中添加引用

  3. 页面初始化

    html设置:

    js初始化:

  4. 获取和设置编辑器的值

    获取编辑器的值:

    1. 直接通过editor获取

    var html = editor.html();

    1. 先把数据同步到textarea中,再获取textarea的值

      editor.sync();

      //原生js获取

      var html = document.getElementById("editor").value;

      //jquery获取

      var html =$("#editor").val();

      //KindEditor 方式

      var html = K('#editor').val();

????设置编辑器的值:

????????editor.html('html内容');

上传文件处理程序

参考所给的示例,只是对示例加以验证,验证是否有上传权限

上传文件列表处理程序

获取上传文件列表同样的,首先进行权限验证:

  1 public class UploadFileHandler : IHttpHandler,IRequiresSessionState
  2     {
  3         private static HttpResponse Response = null;
  4         //最大文件大小
  5         const int MAXFILESIZE = 10240*1024  6 
  7         void ProcessRequest(HttpContext context)
  8         {
  9             验证上传权限
 10             if (context.Session["User"] == )
 11             {
 12                 context.Response.Write(no permission");
 13                 context.Response.End();
 14                 return 15             }
 16             Response = context.Response;
 17             string flag = context.Request[customUpload];
 18             从配置文件中获取网站首页路径
 19             String aspxUrl = Common.ConfigurationHelper.AppSetting(HomeUrlInfo 20             文件保存目录路径
 21             System.Text.StringBuilder savePath = new System.Text.StringBuilder(/Upload/ 22             try
 23  24                 定义允许上传的文件扩展名
 25                 Hashtable extTable = new Hashtable();
 26                 extTable.Add(image",jpg,jpeg,png,bmp 27                 extTable.Add(flashswf,flv 28                 extTable.Add(media 29                 extTable.Add(filedoc,docx,xls,xlsx,ppt,pptx,txt,zip,rar 30                 获取上传文件
 31                 HttpPostedFile imgFile = context.Request.Files[imgFile 32                 if (imgFile ==  33                 {
 34                     imgFile = context.Request.Files[Filedata 35                 }
 36                 当前时间字符串
 37                 string timeString = DateTime.Now.ToString(yyyyMMddHHmmssfff 38                 设置存储目录
 39                 String dirName = context.Request.QueryString[dir 40                 if (String.IsNullOrEmpty(dirName))
 41  42                     dirName =  43  44                 if (!extTable.ContainsKey(dirName))
 45  46                     showError(目录名不正确 47  48                 if (imgFile.InputStream == null || imgFile.InputStream.Length > MAXFILESIZE)
 49  50                     showError(上传文件大小超过限制 51  52                 获取文件扩展名
 53                 string fileExt = Path.GetExtension(imgFile.FileName).ToLower();
 54                 if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','),fileExt.Substring(1).ToLower()) == -1 55  56                     showError(上传文件扩展名是不允许的扩展名。n只允许" + ((String)extTable[dirName]) + 格式。 57  58                 创建文件夹
 59                 savePath.Append(dirName + / 60                 string serverPath = Common.PathHelper.MapPath(savePath.ToString());
 61                 Directory.Exists(serverPath))
 62  63                     Directory.CreateDirectory(serverPath);
 64  65                 String newFileName = timeString + fileExt;
 66                 String filePath = serverPath + newFileName;
 67                 保存到服务器端
 68                 imgFile.SaveAs(filePath);
 69                 savePath.Append(newFileName);
 70                 文件相对网站的虚拟路径
 71                 String fileUrl = savePath.ToString();
 72                  (String.IsNullOrEmpty(flag))
 73  74                     fileUrl = aspxUrl + 75  76                 Hashtable hash =  77                 hash[error"] = 0 78                 hash[url"] = fileUrl;
 79                 context.Response.AddHeader(Content-Typetext/html; charset=UTF-8 80                 context.Response.Write(Common.ConverterHelper.ObjectToJson(hash));
 81  82 
 83  84             catch (System.Threading.ThreadAbortException)
 85  86 
 87  88              (HttpException ex)
 89  90                 context.Response.Write("Error");
 91                 记录日志
 92                 new Common.LogHelper(typeof(UploadFileHandler)).Error(ex);
 93  94              (Exception ex)
 95  96                  97                  98                  99 100         }
101 
102 
103         void showError(string message)
104 105             Hashtable hash = 106             hash[107             hash[message message;
108             Response.AddHeader(109             Response.Write(Common.ConverterHelper.ObjectToJson(hash));
110             Response.End();
111         }
UploadFileHandler Code

using System;
 System.Collections;
  3  System.Collections.Generic;
  4  System.IO;
  5  System.Text.RegularExpressions;
  6  System.Web;
  7  System.Web.SessionState;
  8 
  9 namespace WebApplication1.Admin
 10 {
 11      FileManagerHandler : IHttpHandler,1)"> 12  13          14  15             验证权限
 16              17  18                 context.Response.Write( 19  20                  21 从配置文件中读取网址信息
 23             String aspxUrl = Common.ConfigurationHelper.AppSetting( 24             根目录路径,相对路径
 25             String rootPath =  26             根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/
 27             String rootUrl = aspxUrl + Upload/ 28             图片扩展名
 29             String fileTypes =  30             String currentPath = "" 31             String currentUrl =  32             String currentDirPath =  33             String moveupDirPath =  34              36                 String dirPath = context.Server.MapPath(rootPath);
 37                 String dirName = context.Request.QueryString[String.IsNullOrEmpty(dirName))
 39  40                     if (Array.IndexOf(image,flash,media,file".Split(                    {
 42                         context.Response.Write(Invalid Directory name.                        context.Response.End();
 44                     }
 45                     dirPath += dirName +  46                     rootUrl += dirName +  47                     Directory.Exists(dirPath))
 48                         Directory.CreateDirectory(dirPath);
 50  52 
根据path参数,设置各路径和URL
 54                 String path = context.Request.QueryString[path 55                 path = String.IsNullOrEmpty(path) ?  : path;
 56                 if (path ==  58                     currentPath = dirPath;
 59                     currentUrl = rootUrl;
 60                     currentDirPath =  61                     moveupDirPath =  63                 else
 65                     currentPath = dirPath + path;
 66                     currentUrl = rootUrl + 67                     currentDirPath = 68                     moveupDirPath = Regex.Replace(currentDirPath,1)">@"(.*?)[^/]+/$$1 70 
 71                 排序形式,name or size or type
 72                 String order = context.Request.QueryString[order 73                 order = String.IsNullOrEmpty(order) ?  : order.ToLower();
 74 
 75                 不允许使用..移动到上一级目录
 76                 if (Regex.IsMatch(path,1)">..))
 77  78                     context.Response.Write(Access is not allowed. 79                     context.Response.End();
 81                 最后一个字符不是/
 82                 if (path != "" && !path.EndsWith( 84                     context.Response.Write(Parameter is not valid. 86  87                 目录不存在或不是目录
 88                 Directory.Exists(currentPath))
 90                     context.Response.Write(Directory does not exist. 91  92  93 
 94                 遍历目录取得文件信息
 95                 string[] dirList = Directory.GetDirectories(currentPath);
string[] fileList = Directory.GetFiles(currentPath);
排序方式
switch (order)
100                     case size:
101                         Array.Sort(dirList, NameSorter());
102                         Array.Sort(fileList,1)"> SizeSorter());
103                         break104                     type105                         Array.Sort(dirList,1)">106                         Array.Sort(fileList,1)"> TypeSorter());
107                         108                     name109                     default110                         Array.Sort(dirList,1)">111                         Array.Sort(fileList,1)">112                         113 114                 Hashtable result = 115                 result[moveup_dir_path moveupDirPath;
116                 result[current_dir_path currentDirPath;
117                 result[current_url currentUrl;
118                 result[total_count"] = dirList.Length + fileList.Length;
119                 List<Hashtable> dirFileList = new List<Hashtable>();
120                 result[file_list dirFileList;
121                 for (int i = 0; i < dirList.Length; i++122 123                     DirectoryInfo dir =  DirectoryInfo(dirList[i]);
124                     Hashtable hash = 125                     hash[is_dir"] = true126                     hash[has_file"] = (dir.GetFileSystemInfos().Length > 127                     hash[filesize128                     hash[is_photofalse129                     hash[filetype130                     hash[filename dir.Name;
131                     hash[datetime"] = dir.LastWriteTime.ToString(yyyy-MM-dd HH:mm:ss132                     dirFileList.Add(hash);
133 134                 0; i < fileList.Length; i++135 136                     FileInfo file =  FileInfo(fileList[i]);
137                     Hashtable hash = 138                     hash[139                     hash[140                     hash[ file.Length;
141                     hash["] = (Array.IndexOf(fileTypes.Split(1).ToLower()) >= 142                     hash["] = file.Extension.Substring(143                     hash[ file.Name;
144                     hash["] = file.LastWriteTime.ToString(145 146 147                 context.Response.AddHeader(application/json; charset=UTF-8148                 context.Response.Write(Common.ConverterHelper.ObjectToJson(result));
149 150 151              (IOException ex)
152 153                 context.Response.Write(Error154                 155                 (FileManagerHandler)).Error(ex);
156 157              (SystemException ex)
158 159                 context.Response.Write(160                 161                 162 163             164 165                 context.Response.Write(166                 167                 168 169 170 
171         bool IsReusable
172 173             get
174 175                 return 176 177 178 
179     }
180 
181      NameSorter : IComparer
182 183         int Compare(object x,1)">object y)
184 185             if (x == null && y == 186 187                 return 188 189             190 191                 return -192 193             if (y == 194 195                 196 197             FileInfo xInfo =  FileInfo(x.ToString());
198             FileInfo yInfo =  FileInfo(y.ToString());
199 
200              xInfo.FullName.CompareTo(yInfo.FullName);
201 202 203     
204      SizeSorter : IComparer
205 206         207 208             209 210                 211 212             213 214                 215 216             217 218                 219 220             FileInfo xInfo = 221             FileInfo yInfo = 222 
223              xInfo.Length.CompareTo(yInfo.Length);
224 225 226     
227      TypeSorter : IComparer
228 229         230 231             232 233                 234 235             236 237                 238 239             240 241                 242 243             FileInfo xInfo = 244             FileInfo yInfo = 245 
246              xInfo.Extension.CompareTo(yInfo.Extension);
247 248 249 }
FileManagerHandler Code

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读