当前位置:硬件测评 > .net core CKEditor 图片上传

.net core CKEditor 图片上传

  • 发布:2023-10-05 07:54

-->

最近在玩www.sychzs.cn core。我不想使用UEditor,但想使用CKEditor。因此需要图片上传功能。

废话不多说,下面是效果图:

CKEditor前端代码:


CKeditor config.js配置代码:需要配置图片上传路径

CKEDITOR.editorConfig = 函数( config ) {
// 在此定义对默认配置的更改。例如:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
config.baseHref = "http://" + www.sychzs.cn; config.filebrowserImageUploadUrl = '/Upload/UploadImage';
config.font_names = '歌曲类型/歌曲类型;黑型/黑型;启型/启型;幽源/幽源;微软雅黑/微软雅黑;' + config.font_names;
config.allowedContent = true;
};

如上面的代码所示,我们使用UploadController的UploadImage方法来处理上传事件。

服务器代码:

使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Threading.Tasks;
使用 Microsoft.AspNetCore.Mvc;
使用 Microsoft.AspNetCore.Hosting;
使用 www.sychzs.cn;
使用 Ratuo.Common;
命名空间 Ratuo.Web.Controllers
{
公共类UploadController:控制器
{
私有 IHostingEnvironment _env;
公共UploadController(IHostingEnvironment env)
{
_env = env;
} 公共异步任务 UploadImage()
{
string callback = Request.Query["CKEditorFuncNum"];//要求返回值
var upload = Request.Form.Files[];
string tpl = "";
if (上传==空)
return Content(string.Format(tpl, "", callback, "请选择一张图片!"), "text/html");
//判断是否是图片类型
列表 imgtypelist = 新列表 { "image/pjpeg", "image/png", "image/x-png", "image/gif", "image/bmp" };
if(imgtypelist.FindIndex(x=>x == upload.C​​ontentType) == -)
{
return Content(string.Format(tpl, "", callback, "请上传一张图片!"), "text/html");
}
var data = Request.Form.Files["上传"];
字符串文件路径 = _env.WebRootPath+"\\userfile\\images";
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(upload.FileName);
string fullpath = Path.Combine(filepath, imgname);
试试
{
if (!Directory.Exists(文件路径))
Directory.CreateDirectory(文件路径);
if (数据!= null)
{
等待任务.运行(()=>
{
使用 (FileStream fs = new FileStream(fullpath, FileMode.Create))
{
data.CopyTo(fs);
}
});
}
}
catch(例外情况)
{
return Content(string.Format(tpl, "", callback, "图片上传失败:"+ ex.Message), "text/html");
}
return Content(string.Format(tpl, "/userfile/images/" + imgname, callback, ""), "text/html");
}
}
}

编译并预览。就是这样!

-->

相关文章