通过 nuget 安装 UEditorNetCore
从 UEditor 官网 下载最新的包 ueditorx_x_x_x-utf8-net.zip
解压包,并复制到项目的 wwwroot/lib 目录下,删除 net 目录。
根据 UEditorNetCore 官方的使用说明进行操作
步骤中,控制器 UEditorController 接替原 controller.ashx 的功能,用于统一处理上传逻辑
原 config.json 复制到项目根目录,用于配置上传相关设置(若更改文件名和路径在 services.AddUEditorService() 中处理)
个人喜欢将 xxxPathFormat 值改为:upload/ueditor/{yyyy}{mm}{dd}/{time}{rand:6},方便日后迁移附件后进行批量替换。
上传相关的身份验证和授权控制请在 UEditorController 中处理,如:
public void Do() { if (HttpContext.Request.Query["action"] == "config" || 用户已登录) { _ue.DoAction(HttpContext); } }
因 UEditorNetCore 不能配置 FTP 和 OSS 相关的账号信息,若需要使用该功能,可下载 github 的源代码,将核心项目加入到您的项目中进行修改,具体步骤如下:
打开 Consts.cs,将
public const string xxx = "";
改为
public string xxx { get; set; }
打开 Config.cs,添加两个属性:
public static Consts.ImgFtpServer ftpServer { get; set; } public static Consts.AliyunOssServer aliyunOssServer { get; set; }
打开 UEditorServiceExtension.cs,添加两个参数:
Consts.ImgFtpServer ftpServer = null, Consts.AliyunOssServer aliyunOssServer = null
并赋值给 Config:
Config.ftpServer = ftpServer; Config.aliyunOssServer = aliyunOssServer;
在 Handlers 中,查找“AliyunOssUpload.UploadFile”,将相关代码修改如下:
if (Config.aliyunOssServer == null) { throw new Exception("请在添加服务时(services.AddUEditorService)设置阿里云 OSS 账号信息"); } AliyunOssUpload.UploadFile(Config.aliyunOssServer.AccessEndpoint, Config.aliyunOssServer.AccessKeyId, Config.aliyunOssServer.AccessKeySecret, Config.aliyunOssServer.BucketName, key, fileExt, new MemoryStream(uploadFileBytes));
注意,前端抛出异常的内容却是:文件访问出错,请检查写入权限
Startup.cs 中添加服务改为:
var AliyunOSSOptions = Configuration.GetSection("SiteOptions").GetSection("AliyunOSS"); services.AddUEditorService("ueditorconfig.json", aliyunOssServer: new Consts.AliyunOssServer { AccessEndpoint = AliyunOSSOptions.GetSection("Endpoint").Value, AccessKeyId = AliyunOSSOptions.GetSection("AccessKeyId").Value, AccessKeySecret = AliyunOSSOptions.GetSection("AccessKeySecret").Value, BucketName = AliyunOSSOptions.GetSection("BucketName").Value, });
打开 config.json,将 xxxFtpUpload 值设为 true,将 xxxUrlPrefix 的值改为 OSS 对应的 CDN 网址(以 / 结尾)。
其它注意点:
若使用 UEditorNetCore github 提供的源代码类库代替 nuget,且使用本地存储,那么需要将 Handlers 目录中与定义 savePath 相关的代码(查找 var savePath)替换成被注释的行
network-printer
The-requested-URL-returned-error-404-Not-Found
aspnetcore-ef-getColumnBaseName
aliyun-ddos-without-bgp
metronic-menu-nav