摘要

在项目中要用到富文本编辑器,包含上传图片,插入视频等功能。但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 > )
{
var file = files[];
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 = ; 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 core 2.0]Ueditor 图片上传的更多相关文章

  1. 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片

    在"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传"一文中,使用JSAjaxFileUploader这款插件实现了单文 ...

  2. ueditor图片上传插件的使用

    在项目里使用到ueditor图片上传插件,以前图片上传都是直接使用js代码直接上传图片,比较麻烦,而且效率也比较低,而ueditor这款插件完美的解决了这个问题,这个是百度开发的一款富文本编辑器,在这 ...

  3. ueditor图片上传配置

    ueditor图片上传配置文件为ueditor/php/config.json /* 上传图片配置项 */ "imageActionName": "uploadimage ...

  4. asp.net ueditor 图片上传路径问题解决

    最近练习做一个新闻系统,其中不能少了添加新闻和修改新闻的功能 ,而且还要添加图片.添加文字样式, 所以不得不使用富文本编辑器,在kindeditor和ueditor中,选择了目前还在持续更新的百度产品 ...

  5. Asp.Net Mvc 使用WebUploader 多图片上传

    来博客园有一个月了,哈哈.在这里学到了很多东西.今天也来试着分享一下学到的东西.希望能和大家做朋友共同进步. 最近由于项目需要上传多张图片,对于我这只菜鸟来说,以前上传图片都是直接拖得控件啊,而且还是 ...

  6. springboot+UEditor图片上传

    springboot+UEDitor百度编辑器整合图片上记录于此 1.下载ueditor插件包,解压到static/ueditor目录下 2.在你所需实现编辑器的页面引用三个JS文件 1)  uedi ...

  7. ASP.NET工作笔记之一:图片上传预览及无刷新上传

    转自:http://www.cnblogs.com/sibiyellow/archive/2012/04/27/jqueryformjs.html 最近项目里面涉及到无刷新上传图片的功能,其实也就是上 ...

  8. 百度UEditor图片上传或文件上传路径自定义

    最近在项目中使用到百度UEditor的图片以及文件上传功能,但在上传的时候路径总是按照预设规则来自动生成,不方便一些特殊文件的维护.于是开始查看文档和源代码,其实操作还是比较简单的,具体如下: 1.百 ...

  9. CKEditor5 + vue2.0 自定义图片上传、highlight、字体等用法

    因业务需求,要在 vue2.0 的项目里使用富文本编辑器,经过调研多个编辑器,CKEditor5 支持 vue,遂采用.因 CKEditor5 文档比较少,此处记录下引用和一些基本用法. CKEdit ...

随机推荐

  1. WPF UI 开源专贴

    1.ReactiveUI https://github.com/reactiveui/ReactiveUI http://www.reactiveui.net A MVVM framework tha ...

  2. 002_CentOS-6.4-x86_64安装包的说明

    http://mirrors.sohu.com/centos/6.6/isos/x86_64/?qq-pf-to=pcqq.group //souhu镜像下载地址 0_README.txt 25-Oc ...

  3. The project cannot be built until its prerequisite base-service is built. Cleaning and building all projects is recommended

    参考网址:http://chiangfai.iteye.com/blog/2223661,谢谢! 果然如文中所述,close,重新编译即可!

  4. 几个比较实用的CSS

    1.filter:chroma(color:#FFFFFF);    让指定的背景色透明,例: <table cellspacing = "0" cellpadding = ...

  5. POJ 3281 Dining(最大流+拆点)

    题目链接:http://poj.org/problem?id=3281 题目大意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 1 ...

  6. fis3-postpackager-loader

    静态资源前端加载器,用来分析页面中使用的和依赖的资源(js或css), 并将这些资源做一定的优化后插入页面中.如把零散的文件合并. 注意 此插件做前端硬加载,适用于纯前端项目,不适用有后端 loade ...

  7. mysql5.6.34在默认配置文件修改字符集为utf8后重启mysql服务没效果

    1:事情是这样的,我下载了一个mysql5.6.34版本(windows版本的),下载下来后里面只有个my-default.ini,然后我就直接在my-default.ini 里面配置basedir, ...

  8. 用 scikit-learn 和 pandas 学习线性回归

      用 scikit-learn 和 pandas 学习线性回归¶ from https://www.cnblogs.com/pinard/p/6016029.html 就算是简单的算法,也需要跑通整 ...

  9. rhev 虚拟化

    引用自:https://blog.csdn.net/Jmilk/article/details/50964121#rhev-hhypervisor-%E8%99%9A%E6%8B%9F%E6%9C%B ...

  10. SpringMVC框架07——服务器端JSR303数据校验

    1.数据校验概述 数据校验分为客户端校验和服务器端校验,客户端主要是通过过滤正常用户的误操作,是第一道防线,一般使用JavaScript代码实现.但是只有客户端校验是不够的,攻击者可以绕过客户端验证直 ...