在layui中使用kindeditor富文本编辑器(.net core)


layui 提供了丰富的内置模块,使用轻便,但有不足的是,富文本编辑器所提供的功能太少。这就可能需要引入其他的编辑器。

 

一、去官网下载kindeditor文件,放入wwwroot中。

 

二、在需要添加编辑器的页面,引用相应的js文件

<script charset="utf-8" src="~/kindeditor/kindeditor-all.js"></script>
<script charset="utf-8" src="~/kindeditor/lang/zh-CN.js"></script>
<link rel="stylesheet" href="~/kindeditor/themes/default/default.css" />

 

在需要编辑器的地方加入textarea标签

<textarea id="detail_desc" name="Body" lay-verify="content"></textarea>

 

因为我是把编辑器放在lauyi的表单中的,所以我会加入一些lauyi元素的代码,主要是为了契合表单。但其实只要textarea标签就可以了

 

三、写入相关的js代码

<script type="text/javascript">
KindEditor.ready(function (K) {
var editor = K.create('#detail_desc', {
height: '500px',
uploadJson: '/Manage/SolvingProgramme/KindeditorPicUpload',
fileManagerJson: '/Manage/SolvingProgramme/KindeditorPicUpload',
allowFileManager: true
});

});

</script>

detail_desc就是textarea标签的那个id

四、.net core后端部分

完成了前面,富文本编辑器就出来了,但如果需要上传图片,则需要写后端代码。我现在用的是.net core ,所以我用.net core的方式来实现后端的上传代码功能。

[HttpPost("KindeditorPicUpload")]
public IActionResult KindeditorPicUpload(IList<IFormFile> imgFile, string dir)
{
PicUploadResponse rspJson = new PicUploadResponse() { error = 0, url = "/SolvingProgramme/" };
long size = 0;
string tempname = "";
foreach (var file in imgFile)
{
var filename = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
var extname = filename.Substring(filename.LastIndexOf("."), filename.Length - filename.LastIndexOf("."));
var filename1 = System.Guid.NewGuid().ToString() + extname;
tempname = filename1;
var path = _hostingEnvironment.WebRootPath;
filename = _hostingEnvironment.WebRootPath + $@"\images\SolvingProgramme\{filename1}";
size += file.Length;
using (FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
//这里是业务逻辑
}
}
rspJson.error = 0;
rspJson.url = $@"../../images/SolvingProgramme/" + tempname;
return Json(rspJson);
}

 

/SolvingProgramme/、/images/SolvingProgramme/都是文件的保存路径,可根据自己的需要去调整。

五、编辑功能(后台数据的回显)

如果需要从数据库中读取数据,则只需要加这行代码

 $("#detail_desc").html('@Model.SolvingProgramme.Body');

但要切记这行代码加的地方。是需要加到生成编辑器的前面,如图:

这样,即可实现后台数据对编辑器的回显

这就是在.net core中使用kindeditor的基本方法

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM