.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_ ...
随机推荐
- Windows Zip/CentOS/Radhat系统安装Mysql5.7.x方法
CentOS/Redhat 安装: wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm rpm -Uvh ...
- video 标签在微信浏览器的问题解决方法
最近做的些web页面,内嵌许多小视频,在ios和安卓手机上播放时,遇到不少问题: 在微信浏览器内播放时,视频会自动全屏 如果每个视频都有一张自定义的图片作为封面,在显示视频的同时,如果没有给这个视频设 ...
- vue使用axios请求后端数据
1. 安装axios $ npm install axios 2.在main.js里面导入axios import axios from 'axios' Vue.prototype.$http = a ...
- JAVA多线程的问题以及处理(二)【转】
使用互斥解决多线程问题是一种简单有效的解决办法,但是由于该方法比较简单,所以只能解决一些基本的问题,对于复杂的问题就无法解决了. 解 决多线程问题的另外一种思路是同步.同步是另外一种解决问题的思路,结 ...
- QQ空间、新浪微博、腾讯微博等一键分享API链接代码
1.新浪微博:http://service.weibo.com/share/share.php?url= count=表示是否显示当前页面被分享数量(1显示)(可选,允许为空)&url=将页面 ...
- EOS之eosio.token合约的部署和发放token
eosio.token智能合约 在eos目录中自带的合约中,有一个eosio.token智能合约,这个智能合约的功能是为账户发放token,token可以用来转账操作. 操作步骤 在eos私有节点操作 ...
- Xmanager power suit 6 最新版注册激活
Xmanager Power Suit 6.0.0012 最新版注册激活,长期更新 操作步骤 Xmanger Power Suit 官方 其实有两种 .exe 文件,一个是用于试用的,在注册的时候不能 ...
- 在MFC Dialog中显示cmd窗口
打开Project -> Properties,在Build Events -> Post-Build Event里的Command Line中输入: editbin /SUBSYSTEM ...
- February 28th, 2018 Week 9th Wednesday
Knowledge makes humble, ignorance makes proud. 博学使人谦逊,无知使人骄傲. Humility is not equal with being passi ...
- Unity 琐碎5 : 利用反射设置编辑器参数
问题 最近处理unity资源打包问题时候经常遇到的一个问题就是平台切换和Bundle编译.一般情况下,平台转换我需要依赖Cache Serbver加快转换速度,但是在Build Bundle的时候我又 ...