- https://www.cnblogs.com/wangdongying/p/10843373.html 解决接口context 上下文问题
- https://www.cnblogs.com/redmoon/p/4608174.html UMEditor配置图片上传接口
- https://www.cnblogs.com/shisanmu/p/4133535.html JS 获取根目录
- https://blog.csdn.net/qq_39098505/article/details/85057717 UMEditor返回图片地址
- https://blog.csdn.net/xianglikai1/article/details/77894359 layui富文本图片配置。也是看这个才明白的
F.HtmlEditor().Label("文本编辑器").ID("HtmlEditor1").Height(200).ToolbarSet(FineUIMvc.EditorToolbarSet.Full).Editor(Editor.UMEditor).BasePath(Url.Content("~/res/third-party/umeditor"))
//图片上传配置区 , imageUrl: window.document.location.href.substring(0, window.document.location.href.indexOf(window.document.location.pathname)) +"/api/WebApi/TempTest" //图片上传提交地址,这边获取到根目录加上API接口。 如果写"Test"直接引用当前页面的控制器,这边建议写成API接口,方便后期调用 , imagePath: "" //图片修正地址,引用了fixedImagePath,如有特殊需求,可自行配置。为空,在返回值那边写路径即可 ,imageFieldName:"upfile" //图片数据的key,若此处修改,需要在后台对应文件修改对应参数
返回的Json格式如下。但是不能直接返回Json,要设置到respone响应头中返回
{"state": "SUCCESS","original": "80px - \u526f\u67.jpg","size": "13252","title": "1465.jpg","type": ".jpg","url": "/ueditor/jsp/upload/image/20112/146075274.jpg"}
重点在于这个方法
//API方法 public void TempTest() { //获取上下文 HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; var file = context.Request.Files[0]; //获取选中文件 // Stream stream = file.InputStream; //将文件转为流 Random ran = new Random((int)DateTime.Now.Ticks);//利用时间种子解决伪随机数短时间重复问题 //文件保存位置及命名,精确到毫秒并附带一组随机数,防止文件重名,数据库保存路径为此变量 string serverPath = "/imgUploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + ran.Next(99999) + ".jpg"; //路径映射为绝对路径 string path = context.Server.MapPath(serverPath); JObject json = new JObject(); try { file.SaveAs(path);//图片保存,JPEG格式图片较小 json.Add("state", "SUCCESS"); json.Add("original", "asd"); json.Add("size", "181KB"); json.Add("url", serverPath); json.Add("title", "阿萨德"); json.Add("type", ".jpg"); } catch { } context.Response.Write(json.ToString());//输出结果 context.Response.End(); }//控制器方法 [HttpPost] public void Test() { var context = HttpContext; var file = context.Request.Files[0]; //获取选中文件 Random ran = new Random((int)DateTime.Now.Ticks);//利用时间种子解决伪随机数短时间重复问题 string serverPath = "/imgUploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + ran.Next(99999) + ".jpg"; //路径映射为绝对路径 string path = context.Server.MapPath(serverPath); JObject json = new JObject(); try { file.SaveAs(path);//图片保存,JPEG格式图片较小 json.Add("state", "SUCCESS"); json.Add("original", "asd"); json.Add("size", "181KB"); json.Add("url", serverPath); json.Add("title", "阿萨德"); json.Add("type", ".jpg"); } catch { } context.Response.Write(json.ToString());//输出结果 context.Response.End(); }