无为清净楼资源网 Design By www.qnjia.com
本文实例为大家分享了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.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】
- 赞多《序章》[FLAC/分轨][129.13MB]
- 群星《完美的她 影视原声带》[320K/MP3][51.98MB]
- 群星《完美的她 影视原声带》[FLAC/分轨][141.05MB]
- 山风点伙.1997-深藏不露【立得】【WAV+CUE】
- 李幸倪.2018-First.Of.All.Live.Concert.3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】
- 群星《你就在我身边 电影原声专辑》[320K/MP3][26.68MB]
- 群星《你就在我身边 电影原声专辑》[FLAC/分轨][70.27MB]