.net core CKEditor 图片上传
最近在玩 asp.net core,不想用UEditor,想使用CKEditor。故需要图片上传功能。
废话不多说,先上效果图:

CKEditor 前端代码:
<text id="content" name="content"></text>
<script>
CKEDITOR.replace('content');
</script>
CKeditor config.js 配置代码:需要配置图片上传路径
CKEDITOR.editorConfig = function( config ) {
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
config.baseHref = "http://" + location.host;
config.filebrowserImageUploadUrl = '/Upload/UploadImage';
config.font_names = '宋体/宋体;黑体/黑体;楷体/楷体;幼圆/幼圆;微软雅黑/微软雅黑;' + config.font_names;
config.allowedContent = true;
};
如上代码,我们使用UploadController的UploadImage方法来处理上传事件。
服务端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using Ratuo.Common;
namespace Ratuo.Web.Controllers
{
public class UploadController : Controller
{
private IHostingEnvironment _env;
public UploadController(IHostingEnvironment env)
{
_env = env;
} public async Task<IActionResult> UploadImage()
{
string callback = Request.Query["CKEditorFuncNum"];//要求返回值
var upload = Request.Form.Files[];
string tpl = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(\"{1}\", \"{0}\", \"{2}\");</script>";
if (upload == null)
return Content(string.Format(tpl, "", callback, "请选择一张图片!"), "text/html");
//判断是否是图片类型
List<string> imgtypelist = new List<string> { "image/pjpeg", "image/png", "image/x-png", "image/gif", "image/bmp" };
if(imgtypelist.FindIndex(x=>x == upload.ContentType) == -)
{
return Content(string.Format(tpl, "", callback, "请上传一张图片!"), "text/html");
}
var data = Request.Form.Files["upload"];
string filepath = _env.WebRootPath+"\\userfile\\images";
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(upload.FileName);
string fullpath = Path.Combine(filepath, imgname);
try
{
if (!Directory.Exists(filepath))
Directory.CreateDirectory(filepath);
if (data != null)
{
await Task.Run(() =>
{
using (FileStream fs = new FileStream(fullpath, FileMode.Create))
{
data.CopyTo(fs);
}
});
}
}
catch (Exception ex)
{
return Content(string.Format(tpl, "", callback, "图片上传失败:"+ ex.Message), "text/html");
}
return Content(string.Format(tpl, "/userfile/images/" + imgname, callback, ""), "text/html");
}
}
}
编译,预览一下。即可!
.net core CKEditor 图片上传的更多相关文章
- springMVC和ckeditor图片上传
springMVC和ckeditor图片上传 http://blog.csdn.net/liuchangqing123/article/details/45270977 修正一下路径问题: packa ...
- 简单2步实现 asp.net mvc ckeditor 图片上传
1.打开ckeditor 包下的 config.js,添加一句 配置(PS:ckeditor 很多功能都在该配置文件里配置),如下: config.filebrowserImageUploadUrl ...
- CKEditor图片上传实现详细步骤(使用Struts 2)
本人使用的CKEditor版本是3.6.3.CKEditor配置和部署我就不多说. CKEditor的编辑器工具栏中有一项“图片域”,该工具可以贴上图片地址来在文本编辑器中加入图片,但是没有图片上传. ...
- CKEditor图片上传问题(默认安装情况下编辑器无法处理图片),通过Base64编码字符串解决
准备做一个文章内容网站,网页编辑器采用CKEditor,第一次用,默认安装情况下,图片无法插入,提示没有定义上传适配器(adapter),错误码提示如下: 根据提示,在官网看到有两种途径:一使用CKE ...
- CKEditor 图片上传
可以做如下配置: CKEDITOR.replace('editor1',{ filebrowserBrowseUrl:'/browser/browse.php', filebrowserUploadU ...
- C# MVC 使用 CKEditor图片上传 提示“不正确的服务器响应”
重点:看一下你使用的CKEditor版本 过程: 后台需要一款富文本编辑器.经过挑选后,最后选择了FCKEditor 的升级版 CKEditor .在官网下载了4.10.1版本. 经过一番配置后,富文 ...
- ckeditor图片上传二三事
最近实验室要用ckeditor,踩了几个小坑记录下. 1.出现iframe跨域问题 response.setHeader("X-Frame-Options", "SAME ...
- Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程
Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...
- 使用struts2完成ckeditor和图片上传
代码地址如下:http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor_ ...
随机推荐
- 关于正餐智能POS6.0.1.1改版后,点击反结账进入点菜界面后无法进行加菜的FAQ
适用版本:智能POS正餐V6.0.1.1+适用情况:点击反结账进入点菜界面后无法进行加菜.原因:为让报表统计的数据更准确解决方案:1.点击反结账,输入用户密码,进入点菜界面. 2.点击结算,进入收银台 ...
- 基于pygame实现飞机大战【面向过程】
一.简介 pygame 顶级pygame包 pygame.init - 初始化所有导入的pygame模块 pygame.quit - uninitialize所有pygame模块 pygame.err ...
- python 函数参数为*和**的作用与区别
def function(*args):print(args) 中*的作用:表示此时参数为一个元祖. def function(**args):print(args)中**的作用:表示此时参数为一个字 ...
- AppiumLibrary常用关键字
通过上一章节,open application关键字的使用,相信大家对手机自动化充满了兴趣,那么今天这一章节,主要介绍AppiumLibrary中常用关键字的使用. 一.实用函数 关键字 含义 实例 ...
- es6的正则扩展笔记之修饰符
es6对于正则表达式添加了 u 修饰符和 y 修饰符. u 修饰符:含义为“Unicode模式”,用来正确处理大于\uFFFF的Unicode字符. 该修饰符不光会正确处理正则表达式,还会正确处 ...
- 一张有料的图片!!!附文件-图片合成器C语言实现算法
凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1.打开F盘,先找两个文件,一个是压缩文件(2.rar),另一个是一张图片(1.jpg),将其放到F盘目录下, ...
- Java高级教程02
目录 1.Java线程 1.1. 多线程和多进程 1.2. 线程的执行过程: 1.3. 创建线程的方法 (1). 方法1:通过run() (2). 方法2: 复写Runnable接口(推荐) 1.4. ...
- M码小黄衫买家秀=w=
M码小黄衫买家秀=w= 17°的天气穿不了短袖polo..就只能这样强行上图啦~ 因为我一直耿耿于大一面向对象课上拿到的那件XL码小黄衫,长到能穿到膝盖,拍小黄衫全家福时候只能很凄凉的借了件小号的穿, ...
- Codeforces Round #542 B Two Cakes
B. Two Cakes time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 【NOI2019模拟】搬砖
[NOI2019模拟]搬砖 Description 小火车很穷,只好去搬砖了. 小火车被工头要求搭建\(n\)座塔,第i个高度为\(H_i\),也就是由\(H_i\)块砖头组成.每次小火车可以携带至多 ...