本文实例为大家分享了CKEditor与dotnetcore实现图片上传的具体代码,供大家参考,具体内容如下
CKEditor的使用
1.引入js库
<script src="/UploadFiles/2021-04-02/ckeditor.js">2.定义一个textarea标签
<textarea id="editor"> </textarea>3.用CkEditor替换textarea即可使用基本功能
CKEDITOR.replace('editor');4.配置CkEditor
添加图片上传,代码插入工具
CKEDITOR.replace('editor-box', { //GitHub地址:https://github.com/ckeditor toolbar: [ { name: 'document', items: ['Source'] }, { name: 'basicstyles', items: ['Bold', 'Italic'] }, { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote'] }, { name: 'links', items: ['Link', 'Unlink'] }, { name: 'insert', items: ['Image','CodeSnippet'] }, { name: 'styles', items: ['Format', 'Styles'] } ], filebrowserImageUploadUrl: '/Blogs/UploadImageUrl', //配置图片上传后台Url customConfig: '', extraPlugins: 'codesnippet,image2,uploadimage', removePlugins: 'image', //mathJaxLib: 'https://cdn.mathjax.org/mathjax/2.6-latest/MathJax.js"htmlcode">/// <summary> /// 图片上传 /// </summary> /// <param name="env"></param> /// <returns></returns> public async Task<IActionResult> UploadImageUrl([FromServices]IHostingEnvironment env) { // CKEditor提交的很重要的一个参数 string callback = Request.Query["CKEditorFuncNum"]; var form = Request.Form; var img = form.Files[0]; //获取图片 string fileName = img.FileName; var openReadStream = img.OpenReadStream(); byte[] buff = new byte[openReadStream.Length]; await openReadStream.ReadAsync(buff, 0, buff.Length); string filenameGuid = Guid.NewGuid().ToString(); var bowerPath = PathUtils.GetSavePath(filenameGuid, true) + ".jpg";//获取到图片保存的路径,这边根据自己的实现 var savePath = Path.Combine(env.WebRootPath, bowerPath); using (FileStream fs = new FileStream(savePath, FileMode.Create)) { await fs.WriteAsync(buff, 0, buff.Length); //服务器返回JavaScript脚本 string result = $"<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(\"{callback}\", \"{"/"+bowerPath}\", \"\");</script>"; Response.ContentType = "text/html;charset=UTF-8"; return Content(result); } }6.注意
服务器返回需要加上这个,否则会遇到前端页面不执行返回的JavaScript脚本的问题
Response.ContentType = "text/html;charset=UTF-8";配置完成即可使用图片上传功能
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 群星.1993-滚石九大天王十二出好戏·贺岁齐唱【滚石】【WAV+CUE】
- 黑神话悟空上品虫校尉精魄获取方法一览|上品虫校尉精魄收集攻略
- 《指环王:夏尔的传说》推迟至2025年初发行
- 黑神话悟空上品蝎太子精魄获取方法一览|上品蝎太子精魄收集攻略
- 银霞.1977-《台北66电影原声带》台湾复刻版[WAV+CUE]
- 银霞.1980-《你那好冷的小手》台湾珍藏纪念版[WAV+CUE]
- 王菲.2006《菲常好听》引进版[低速原抓WAV+CUE]
- 《三日危情独家的记忆》女角色介绍
- 《天国拯救》拉泰比武大会攻略
- 任天堂推出《马里奥惊奇》NS捆绑包 10月24日发售
- 《孤岛惊魂》能否逆转育碧颓势?曝系列新作正招兵买马
- 天命人呼叫战术轰炸!玩家自制《黑神话:悟空》轰炸机Mod
- 群星.1992-滚石九大天王·十二出好戏【滚石】【WAV+CUE】
- 群星.1994-新滚石九大天王之年度大丫滚石】【WAV+CUE】
- 坣娜.1998-别恋【非常喜】【WAV+CUE】