kindeditor富文本编译器

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() });
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值