虽然官网自带Base64插件,但是需要使用Npm方式引用。
可使用以下方式代替:
页面代码
<!--基础引用-->
<script src="~/plugins/ckeditor5-build-decoupled-document/ckeditor.js"></script>
<script src="~/plugins/ckeditor5-build-decoupled-document/zh-cn.js"></script>
<!--需要额外引用ckfinder.js-->
<script src="https://ckeditor.com/apps/ckfinder/3.5.0/ckfinder.js"></script>
<script>
var ckeditor;
ckeditor = DecoupledEditor.create(document.querySelector('#editor'), {
language: "zh-cn",
ckfinder: { uploadUrl: '/Controller/ActionMethod' }
}).then(editor => {
const toolbarContainer = document.querySelector('.toolbar-container');
toolbarContainer.prepend(editor.ui.view.toolbar.element);
window.editor = editor;
//editor.setData('<p>Some text.</p>');
//const data = editor.getData();
});
</script>
C# 后台 Controller 方法
/// <summary>
/// 将上传的图片转为Base64
/// </summary>
/// <param name="files"></param>
/// <returns></returns>
public JsonResult ArticleEmbedImg()
{
string body = Request.Body.ToString();
if (Request.Form.Files.Count > 0)
{
using (MemoryStream ms = new MemoryStream())
{
Request.Form.Files[0].OpenReadStream().CopyTo(ms);
return Json(new { uploaded = "true", url = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray()) });
}
}
else
{
return Json(null);
}
}
|