kindeditor富文本编译器
1.引入js文件
《script src=“~/assets/plugins/kindeditor/kindeditor-all.js”/》
《script src=“~/assets/plugins/kindeditor/lang/zh-CN.js”/》
2.html
< textarea id=editor_id style=widht:100%;height:400px/>
3.初始化富文本编译器
var editor;
KindEditor.ready(function (K) {
editor = K.create(‘#editor_id’, {
minHeight: ‘350px’,
uploadJson: setter.host + ‘/File/UploadKindEditorFile’,
afterUpload: function (data) {
this.sync();
}, //图片上传后,将上传内容同步到textarea中
afterBlur: function () { this.sync(); }, // 失去焦点时,将上传内容同步到textarea中
});
});
//清空
editor.html(‘’);
//赋值
editor.html(decodeURIComponent(result.data.Content));
//判断是否为空
if (editor.isEmpty()) {
tipsForm(‘error’, ‘请输入内容’);
editor.focus();
return;
}
//提取上传的图片
var content = editor.html(); //全部内容
var ContentImg = []; //存放图片地址path
var newContent = content.replace(/<img [>]*src=[‘"]([^’"]+)[>]*>/gi, function (match, capture) {
console.log(capture);
var val = capture.split(“/Uploads”)[1];
console.log(val);
if (val == undefined || val == “”) {
ContentImg.push({ path: capture });
}
else {
var path = “/Uploads” + val;
ContentImg.push({ path: path });
}
});
console.log(ContentImg);
if (ContentImg.length > 0) {
formData.ContentImg = JSON.stringify(ContentImg); //保存的图片数组 数据库字段保存为nvarchar(max)
}
formData.Content = encodeURIComponent(editor.html());//内容 数据库字段保存为nvarchar(max)
/后台保存上传附件
///
///上传附件
///
///
public JsonResult UploadKindEditorFile()
{
Controller cxt = this;
try
{
string imgPath = “/Uploads/Images/”;
string docPath = “/Uploads/Document/”;
string videoPath = “/Uploads/Video/”;
string otherPath = “/Uploads/Other/”;
string[] imgArr = new string[] { “.gif”, “.jpg”, “.jpeg”, “.png”, “.bmp” };
string[] docArr = new string[] { “.doc”, “.docx”, “.xls”, “.xlsx”, “.pdf”, “.ppt”, “.txt” };
string[] videoArr = new string[] { “.avi”, “.mov”, “.rmvb”, “.rm”, “.flv”, “.mp4”, “.flash” };
string[] otherArr = new string[] { “.zip”, “.rar”, “.7z” };
HttpPostedFileBase uploadFile = null;
//string value1 = cxt.Request[“path”];
//value1 = HttpUtility.UrlDecode(value1);
string path = “”;
string resPath = “”;
uploadFile = cxt.Request.Files[0];
if (uploadFile == null)
{
return Json(new { error = 1, message = “上传失败:未获取到文件信息!” });
}
string extName = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf(‘.’));
if (imgArr.Contains(extName))
{
path = Server.MapPath(imgPath);
resPath = imgPath;
}
else if (docArr.Contains(extName))
{
path = Server.MapPath(docPath);
resPath = docPath;
}
else if (videoArr.Contains(extName))
{
path = Server.MapPath(videoPath);
resPath = videoPath;
}
else
{
path = Server.MapPath(otherPath);
resPath = otherPath;
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string oldName = uploadFile.FileName;
string fileName = Guid.NewGuid().ToString() + uploadFile.FileName.Substring((uploadFile.FileName.LastIndexOf(‘.’)), (uploadFile.FileName.Length - uploadFile.FileName.LastIndexOf(‘.’)));
uploadFile.SaveAs(path + fileName);
return Json(new { error = 0, url = resPath + fileName });
}
catch (Exception ex)
{
return Json(new { error = 1, message = “上传失败!” + ex.ToString() });
}
}