kindEditor编辑器的使用

kindEditor编辑器的使用最近在跟着教程写一些简单的项目的时候用到了

最近在跟着教程写一些简单的项目的时候用到了kindEditor编辑器,在后台添加文章时,使用到了,下面就讲一下使用方法吧。

首先是替换,将文本框替换为编辑器,这里的实现比较简单:

 KindEditor.ready(function (K) { window.editor = K.create('#Article_Content', { height: '500px', uploadJson: '@Url.Action("Upload", "Attachment")', fileManagerJson: '@Url.Action("FileManagerJson", "Attachment")', allowFileManager: true, formatUploadUrl: false });

然后可以选择上传附件什么的,主要方法在Attachment/Upload中,其中_uploadConfg是将配置中的文件读取出来,这里把代码贴上来

public ActionResult Upload() { var _uploadConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").GetSection("UploadConfig") as CMSDemo.Models.Config.UploadConfig; //文件的最大限制 int _maxSize = _uploadConfig.MaxSize; //保存的路径 string _savePath; //文件路径 string _filePath = "~/" + _uploadConfig.Path + "/"; //文件名 string _fileName; //拓展名 string _fileExt; //文件类型 string _dirName; //允许上传的类型 Hashtable extTable = new Hashtable(); extTable.Add("image", _uploadConfig.ImageExt); extTable.Add("flash", _uploadConfig.FileExt); extTable.Add("media", _uploadConfig.MediaExt); extTable.Add("file", _uploadConfig.FileExt); //获取到要上传的文件 HttpPostedFileBase _postFile = Request.Files["imgFile"]; if (_postFile == null) return Json(new { error = 1, message = "请选择文件" }); _fileName = _postFile.FileName; _fileExt = Path.GetExtension(_fileName).ToLower(); _dirName = Request.QueryString["dir"]; if (string.IsNullOrEmpty(_dirName)) _dirName = "image"; if (!extTable.ContainsKey(_dirName)) return Json(new { error = 1, message = "目录类型不存在" }); if (_postFile.InputStream == null || _postFile.InputStream.Length > _maxSize) return Json(new { error = 1, message = "文件大小超过限制" }); if (string.IsNullOrEmpty(_fileExt) || Array.IndexOf(((string)extTable[_dirName]).Split(','), _fileExt.Substring(1).ToLower()) == -1) return Json(new { error = 1, message = "不允许上传此类型的文件。 \n只允许" + ((String)extTable[_dirName]) + "格式。" }); _filePath += _dirName + "/" + DateTime.Now.ToString("yyyy-MM") + "/"; _savePath = Server.MapPath(_filePath); //检查上传目录 if (!Directory.Exists(_savePath)) Directory.CreateDirectory(_savePath); string _newFileName = DateTime.Now.ToString("yyyyMMdd_hhmmss") + _fileExt; _savePath += _newFileName; _filePath += _newFileName; //保存文件 _postFile.SaveAs(_savePath); //保存数据库记录 AttachmentService.Add(new Attachment() { Extension = _fileExt.Substring(1), FilePath = _filePath, Owner = User.Identity.Name, UploadDate = DateTime.Now, Type = _dirName, }); return Json(new { error = 0, url = Url.Content(_filePath) }); }

接着就是附件管理了,方法在Attachment/FileManageJson中,

 public ActionResult FileManagerJson(int? id ,string dir) { Models.AttachmentManagerViewModel _attachmentViewModel; IQueryable 
 
   _attachments; //id为null,表示是modelid为null,此时查询数据库中没有跟模型对应起来的附件列表(已上传,但上传的文章,还未保存) if (id == null) _attachments = AttachmentService.FindList(null, User.Identity.Name, dir); else //id不为null,返回指定模型的id和id为null附件列表 _attachments = AttachmentService.FindList((int)id, User.Identity.Name, dir, true); //初始化一个列表 var _attachmentList = new List 
  
    (_attachments.Count()); foreach(var _attachment in _attachments) { _attachmentViewModel = new Models.AttachmentManagerViewModel() { datetime = _attachment.UploadDate.ToString("yyyy-MM-dd-HH-mm-ss"), filetype = _attachment.Extension, has_file = false, is_dir = false, is_photo = _attachment.Type.ToLower() == "image" ? true : false, filename = Url.Content(_attachment.FilePath), // Filesize=0 }; FileInfo _fileInfo = new FileInfo(Server.MapPath(_attachment.FilePath)); _attachmentViewModel.filesize = (int)_fileInfo.Length; _attachmentList.Add(_attachmentViewModel); } //_attachmentList.Count return Json(new { moveup_dir_path = "", current_dir_path = "", current_url = "", total_count = _attachmentList.Count, file_list = _attachmentList },JsonRequestBehavior.AllowGet); } 
   
 

其中AttachmentManagerViewModel的模型,是官方文档的模型,我修改过模型后不能正常使用了,所以不能随意修改

 public class AttachmentManagerViewModel { public bool is_dir { set; get; } public bool has_file { set; get; } public int filesize { get; set; } public bool is_photo { get; set; } public string filetype { get; set; } public string filename { get; set; } public string datetime { get; set; } }

最后就是创建对话框,来显示我们刚才上传的附件了,我这里的附件主要是图片

 var editor2 = K.editor({ fileManagerJson: '@Url.Action("FileManagerJson", "Attachment")' }); K('#btn_picselect').click(function () { editor2.loadPlugin('filemanager', function () { editor2.plugin.filemanagerDialog({ viewType: 'VIEW', dirName: 'image', clickFn: function (url, title) { var url; $.ajax({ type: "post", url: "@Url.Action("CreateThumbnail", "Attachment")", data: { originalPicture: url }, async: false, success: function (data) { if (data == null) alert("生成缩略图失败!"); else { K('#DefaultPicUrl').val(data); K('#imgpreview').attr("src", data); } editor2.hideDialog(); } }); } }); }); });

官方也有相应的上传附件的方法,需要的可以去官方查阅相关文档









版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/207052.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午2:33
下一篇 2026年3月19日 下午2:33


相关推荐

  • c++语言switch用法举例_switch语句特点

    c++语言switch用法举例_switch语句特点C语言/C++【switch语句详解(用法、规则、流程图、实例)】

    2022年10月9日
    4
  • 新手分享自己使用的在线UML画图工具

    新手分享自己使用的在线UML画图工具刚接触 UML 时间不长 看了 N 多教学视频 下载好了几个软件各种不习惯当我遇见了 ProcessOn nbsp 从此我彻底 爱上 了它 http www processon com nbsp UML 各类例图它几乎全部支持 操作起来灰常的方便 它是一款在线的 UML 作图工具 据说是国产的 官方人员说永久免费 就当支持一下国产了 不过对于新手的我在体验几款确同类软件 个人感觉 Proces

    2026年3月19日
    1
  • Netty 权威指南学习

    Netty 权威指南学习2019-03-04Netty是一个异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。bio和niobio作为阻塞io,一个请求一个线程,应对不了高并发的应用。nio:缓冲区Buffer通道Channel多路复用器Selector…

    2026年4月14日
    5
  • vue slot插槽_笔记本内存条插槽显示4个

    vue slot插槽_笔记本内存条插槽显示4个为什么使用slotslot(插槽)在生活中很多地方都有插槽,电脑usb的插槽,插板当中的电源插槽插槽的目的是为了让我们原来的设备具备更多的扩展性比如电脑的USB我们可以插入U盘,手机,鼠标,键

    2022年7月29日
    10
  • java数字取整(向上取整,向下取整,四舍五入)

    java数字取整(向上取整,向下取整,四舍五入)向上取整:Math.ceil(doublea)向下取整:Math.floor(doublea)四舍五入取整:Math.round(doublea)例:Math.ceil(24.2)–>25Math.floor(24.7)–>24Math.round(24.2)–>24Math.round(24.7)–>25

    2022年6月21日
    43
  • 免费服务器+免费域名 【白嫖手册】

    免费服务器+免费域名 【白嫖手册】前言 白嫖服务器方法 白嫖域名方法 准备工作:一台电脑 一个邮箱 一个QQ号 一个手机号 一毛钱1.白嫖服务器方法友链:免费主机

    2022年6月22日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号