最近在玩 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 图片上传的更多相关文章

  1. springMVC和ckeditor图片上传

    springMVC和ckeditor图片上传 http://blog.csdn.net/liuchangqing123/article/details/45270977 修正一下路径问题: packa ...

  2. 简单2步实现 asp.net mvc ckeditor 图片上传

    1.打开ckeditor 包下的  config.js,添加一句 配置(PS:ckeditor 很多功能都在该配置文件里配置),如下: config.filebrowserImageUploadUrl ...

  3. CKEditor图片上传实现详细步骤(使用Struts 2)

    本人使用的CKEditor版本是3.6.3.CKEditor配置和部署我就不多说. CKEditor的编辑器工具栏中有一项“图片域”,该工具可以贴上图片地址来在文本编辑器中加入图片,但是没有图片上传. ...

  4. CKEditor图片上传问题(默认安装情况下编辑器无法处理图片),通过Base64编码字符串解决

    准备做一个文章内容网站,网页编辑器采用CKEditor,第一次用,默认安装情况下,图片无法插入,提示没有定义上传适配器(adapter),错误码提示如下: 根据提示,在官网看到有两种途径:一使用CKE ...

  5. CKEditor 图片上传

    可以做如下配置: CKEDITOR.replace('editor1',{ filebrowserBrowseUrl:'/browser/browse.php', filebrowserUploadU ...

  6. C# MVC 使用 CKEditor图片上传 提示“不正确的服务器响应”

    重点:看一下你使用的CKEditor版本 过程: 后台需要一款富文本编辑器.经过挑选后,最后选择了FCKEditor 的升级版 CKEditor .在官网下载了4.10.1版本. 经过一番配置后,富文 ...

  7. ckeditor图片上传二三事

    最近实验室要用ckeditor,踩了几个小坑记录下. 1.出现iframe跨域问题 response.setHeader("X-Frame-Options", "SAME ...

  8. Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程

    Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...

  9. 使用struts2完成ckeditor和图片上传

    代码地址如下:http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor_ ...

随机推荐

  1. codeforces 2B The least round way(DP+数学)

    The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...

  2. 远程连接MySQL数据库问题总结

    远程连接MySQL数据库时,陆陆续续遇到了一些杂七杂八的问题,相信很多人也曾经遇到过这类问题,下面总结归纳在下面,方便以后直接查找. 1:出现ERROR 2003 (HY000): Can't con ...

  3. 完全数java

    完全数:小于本身的所有因子的和(包括1) public class test01 { public static void main(String[] args) { Scanner scanner= ...

  4. 移动端 input样式在安卓与ios上不同的解决方案

    input{ -webkit-appearance:none; }

  5. Proxysql读写分离配置

    ProxySQL是Percona主推的读写分离中间件,下载地址为: https://www.percona.com/downloads/proxysql/ 一.安装 1:下载 wget https:/ ...

  6. window.location.href刷新页面

    刷新当前页 window.location.href=window.location.href; 或者 window.location.href="当前URL",例如 window ...

  7. LeetCode算法题-Number of 1 Bits(Java实现)

    这是悦乐书的第186次更新,第188篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第45题(顺位题号是191).编写一个带无符号整数的函数,并返回它所具有的"1 ...

  8. vue-cli笔记

    总是忘,留个笔记. vue-cli A simple CLI for scaffolding Vue.js projects. Installation Prerequisites: Node.js ...

  9. [福大软工] Z班——个人技术博客评分

    个人技术博客 作业地址 https://edu.cnblogs.com/campus/fzu/SoftwareEngineering2015/homework/1070 作业要求 个人技术博客单次作业 ...

  10. jquery中prop()和attr()用法

    jquery1.6中新加了一个方法prop(),一直没用过它,官方解释只有一句话:获取在匹配的元素集中的第一个元素的属性值. 大家都知道有的浏览器只要写disabled,checked就可以了,而有的 ...