[Asp.net core 2.0]Ueditor 图片上传
2018-01-19 15:11
429 查看
摘要
在项目中要用到富文本编辑器,包含上传图片,插入视频等功能。但ueditor只有.net版本,没有支持core。那么上传等接口就需要自己实现了。一个例子
首先去百度ueditor官网下载简化版的ueditor。并引入到项目中如图:
页面引用以下几个文件:
<link href="~/ueditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet" /> <script src="~/ueditor/third-party/jquery.min.js"></script> <script src="~/ueditor/umeditor.config.js" charset="utf-8"></script> <script src="~/ueditor/umeditor.js" charset="utf-8"></script> <script src="~/ueditor/lang/zh-cn/zh-cn.js"></script>
修改ueditor配置文件:
//为编辑器实例添加一个路径,这个不能被注释 UMEDITOR_HOME_URL: URL //图片上传配置区 , imageUrl: "../fileupload/UeditorUpload" //图片上传提交地址 , imagePath: URL + "net/" //图片修正地址,引用了fixedImagePath,如有特殊需求,可自行配置 , imageFieldName: "upfile" //图片数据的key,若此处修改,需要在后台对应文件修改对应参数 //工具栏上的所有的功能按钮和下拉框,可以在new编辑器的实例时选择自己需要的从新定义 , toolbar: [ 'source | undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor | removeformat |', 'insertorderedlist insertunorderedlist | selectall cleardoc paragraph | fontfamily fontsize', '| justifyleft justifycenter justifyright justifyjustify |', 'link unlink | image video |', 'horizontal print preview fullscreen' ]
添加接收文件控制器,并提供接口
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace FireFly.Admin.Controllers { public class FileUploadController : Controller { private IHostingEnvironment hostingEnv; public FileUploadController(IHostingEnvironment env) { hostingEnv = env; } public async Task<IActionResult> UeditorUpload() { var files = Request.Form.Files; string callback = Request.Query["callback"]; string editorId = Request.Query["editorid"]; if (files != null && files.Count > 0) { var file = files[0]; string contentPath = hostingEnv.WebRootPath; string fileDir = Path.Combine(contentPath, "upload"); if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } string fileExt = Path.GetExtension(file.FileName); string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileExt; string filePath = Path.Combine(fileDir, newFileName); using (FileStream fs = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fs); } var fileInfo = getUploadInfo("../../upload/" + newFileName, file.FileName, Path.GetFileName(filePath), file.Length, fileExt); string json = BuildJson(fileInfo); Response.ContentType = "text/html"; if (callback != null) { await Response.WriteAsync(String.Format("<script>{0}(JSON.parse(\"{1}\"));</script>", callback, json)); } else { await Response.WriteAsync(json); } return View(); } return NoContent(); } private string BuildJson(Hashtable info) { List<string> fields = new List<string>(); string[] keys = new string[] { "originalName", "name", "url", "size", "state", "type" }; for (int i = 0; i < keys.Length; i++) { fields.Add(String.Format("\"{0}\": \"{1}\"", keys[i], info[keys[i]])); } return "{" + String.Join(",", fields) + "}"; } /** * 获取上传信息 * @return Hashtable */ private Hashtable getUploadInfo(string URL, string originalName, string name, long size, string type, string state = "SUCCESS") { Hashtable infoList = new Hashtable(); infoList.Add("state", state); infoList.Add("url", URL); infoList.Add("originalName", originalName); infoList.Add("name", Path.GetFileName(URL)); infoList.Add("size", size); infoList.Add("type", Path.GetExtension(originalName)); return infoList; } } }
测试
总结
这里简单实现了ueditor在asp.net core 2.0 web应用中的使用,需要实现的只是文件的上传接口。相关文章推荐
- asp.net 百度编辑器 UEditor 上传图片 图片上传配置 编辑器配置 网络连接错误,请检查配置后重试
- ASP.NET 2.0中向数据库中上传图片
- Asp.net 2.0 无刷新图片上传 显示缩略图 具体实现
- ASP.NET Ueditor上传图片添加水印
- 百度Ueditor编辑器的使用,ASP.NET也可上传图片
- 百度UEditor在线编辑器上传图片提示网络连接错误,请检查配置后重试 asp.net 4.0
- [ueditor] Asp.net上传图片出错问题
- asp.net应用百度编辑器(UEditor)上传图片跟上次附件不成功的解决办法
- asp.net 2.0中判断上传的图片是否符合规格
- asp.net ueditor 图片上传路径问题解决
- asp.net 2.0中判断上传的图片是否符合规格
- asp.net中使用ueditor 1.3.6上传图片问题
- 用VSCode开发一个asp.net core2.0+angular5项目(5): Angular5+asp.net core 2.0 web api文件上传
- C# Asp.net 修改Ueditor编辑器上传图片保存路径
- Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程
- asp.net 2.0中判断上传的图片是否符合规格
- asp.net 2.0实战(2) 在GridView或DetailsView中加入日期选择和图片上传修改的自定义控件_jaye.net
- 『ASP.NET 2.0』FileUpload图片上传自动生成缩略图
- asp.net 2.0 FileUpload控件 上传图片 带预览
- Asp.NetCoreWebApi图片上传接口(二)集成IdentityServer4授权访问(附源码)