服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - ASP.NET教程 - asp.net core集成CKEditor实现图片上传功能的示例代码

asp.net core集成CKEditor实现图片上传功能的示例代码

2021-12-07 15:39UP技术控 ASP.NET教程

这篇文章主要介绍了asp.net core集成CKEditor实现图片上传功能的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

背景

本文为大家分享了asp.net core 如何集成CKEditor ,并实现图片上传功能的具体方法,供大家参考,具体内容如下。

准备工作

1.visual studio 2019 开发环境

2.net core 2.0 及以上版本

实现方法

1、新建asp.net core web项目

asp.net core集成CKEditor实现图片上传功能的示例代码

2、下载CKEditor

这里我们新建了一个系统自带的样本项目,去 CKEditor官网下载一个版本,解压后拷贝大wwwroot中

asp.net core集成CKEditor实现图片上传功能的示例代码

3、增加图片上传控制器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
@using CompanyName.ProjectName.Web.Host.Web.Model
@model demock
@{
  ViewData["title"] = "富文本框";
  Layout = "_SimpleTable";
}
@section headers{
  @*<style>
      .nav.nav-tabs.padding-18 {
        padding-left: 18px;
      }
      .nav-tabs {
        border-color: #c5d0dc;
        margin-bottom: 0 !important;
        margin-left: 0;
        position: relative;
        top: 1px;
      }
</style>*@
}
 
<section class="content">
  <div class="box box-primary">
    <div class="box-header with-border">
      <ul class="nav nav-tabs padding-18">
        <li>
 
          <a href="/demo/sendmail" rel="external nofollow" >
            <i class="orange ace-icon fa fa-cog bigger-120"></i>
 
            发邮件
          </a>
        </li>
        <li class="active">
          <a href="javascript:void(0);" rel="external nofollow" >
 
            <i class="green ace-icon fa fa-home bigger-120"></i>
 
            富文本编辑框
          </a>
        </li>
      </ul>
    </div>
    <!-- /.box-header -->
    <!-- form start -->
    <form role="form" id="form1">
      <div class="box-body">
        <div class="callout callout-success">
          <h4>富文本编辑器</h4>
          <p></p>
        </div>
        <table class="form">
 
          <tr>
            <th class="formTitle">内容</th>
            <td class="formValue">
 
              @Html.EditorFor(model => model.rules)
 
              @*<textarea class="form-control" name="Text" id="Text" style="height: 70px;" placeholder=""></textarea>*@
            </td>
          </tr>
        </table>
      </div>
      <!-- /.box-body -->
      <div class="box-footer">
        <button type="button" id="btnSave" class="btn btn-success">保存</button>
      </div>
    </form>
  </div>
 
  <!-- /.tab-content -->
</section>
 
@section scripts{
 
  <script src="~/ckeditor/ckeditor.js"></script>
  <script src="~/js/jquery.base64.js"></script>
  <script type="text/javascript">
  //editor.document.getBody().getText(); //取得纯文本
  //editor.document.getBody().getHtml(); //取得html文本
  //var editor = CKEDITOR.replace('Text',
  //{
  //  toolbar: "Full",
  //  filebrowserUploadUrl: "/Image/ckupload?type=File", //开启文件上传(此项会同时开启图片和FLASH上传)
  //  filebrowserImageUploadUrl: "/Image/ckupload?type=Images", // 开启图片上传
  //  filebrowserFlashUploadUrl: "/Image/ckupload?type=Flash" //开启FLASH上传
  //});
    $.base64.utf8encode = true;
  var editor2 = CKEDITOR.replace('rules',
    {
      toolbar: "Full",
      filebrowserUploadUrl: "/Image/ckupload?type=File", //开启文件上传(此项会同时开启图片和FLASH上传)
      filebrowserImageUploadUrl: "/Image/ckupload?type=Images", // 开启图片上传
      filebrowserFlashUploadUrl: "/Image/ckupload?type=Flash" //开启FLASH上传
    });
 
  $(function () {
    editor2.setData("@(Html.Raw(Model.rules))");
  });
  $("#btnSave").click(function (ev) {
 
    var a = editor2.document.getBody().getHtml();
 
    $("#rules").val( $.base64.btoa(a) );
    var $form = $("#form1");
    editor2.setData(a);
 
    $.ajax({
      url: "/demo/ck/",
      data: $form.serialize(),
      type: "POST",
      cache: false,
      async: false,
      beforeSend: function () {
        $("#btnSave").attr("disabled", "disabled");
      },
      success: function (r) {
 
       // if (r.isSucceeded == true) {
          // location.href = r.goBackUrl;// "/Employee/index?ram=" + Math.random();// r.GoBackUrl;
        // }
       // else // 其他问题
       //  {
          alert( $.base64.atob(r.message, true));
 
      //  }
        $("#btnSave").removeAttr("disabled");
      },
      error: function () {
 
        alert("保存失败");
        $("#btnSave").removeAttr("disabled");
      }
    });
 
   //  alert(editor2.document.getBody().getHtml());
    // return;
    //$.post("/demo/ck/",
    //  {
 
    //    Text: editor2.document.getBody().getHtml(),
    //  },
    //  function (data, status) {
    //     alert("Data: " + data + "\nStatus: " + status);
 
    //  });
  });
</script>
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/// <summary>
   /// 富文本框 上传图片
   /// </summary>
   /// <returns></returns>
   [HttpPost]
   [IgnoreAuthorize]
   public IActionResult Ckupload()
   {
     uploadfile user = new uploadfile();
     var files = Request.Form.Files;
     if (files == null || files.Count == 0)
       ViewBag.cc = "no file";
     user.ShareImg = files[0];
     var contentRoot = Directory.GetCurrentDirectory();
     var webRoot = Path.Combine(contentRoot, "wwwroot");
     var parsedContentDisposition = ContentDispositionHeaderValue.Parse(user.ShareImg.ContentDisposition);
     var originalName = parsedContentDisposition.FileName.ToString().Replace("\"", "");
     var ext = Path.GetExtension(Path.Combine(webRoot, originalName));
     //  if (ext != ".jpg")
     //  return Json(new { jsonrpc = "2.0", error = new { code = 101, message = "文件格式错误" }, id = "id" });
     string gid = Guid.NewGuid().ToString();
     var fileName = Path.Combine("upload", gid + ext);
     var fileName2 = "upload/" + gid + ext;
     using (var stream = new FileStream(Path.Combine(webRoot, fileName), FileMode.CreateNew))
     {
       user.ShareImg.CopyTo(stream);
     }
     string output = @"<script type=""text/javascript"">window.parent.CKEDITOR.tools.callFunction({0} ,'{1}');</script>";
     string url = "http://" + Request.Host.Value;
     output = string.Format(output, Request.Query["CKEditorFuncNum"], url + "/" + fileName2);
     ViewBag.cc = output;
     return View();
   }

4、增加图片上传控制器注意返回是一个json对象,因此建了一个

简单的对象返回。

运行效果

asp.net core集成CKEditor实现图片上传功能的示例代码

asp.net core集成CKEditor实现图片上传功能的示例代码

源码地址

https://gitee.com/conanOpenSource_admin/CompanyName.ProjectName

到此这篇关于asp.net core集成CKEditor实现图片上传功能的示例代码的文章就介绍到这了,更多相关asp.net core CKEditor图片上传内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://mp.weixin.qq.com/s/1jxNe1XkKX7o1v3HfvYRRw

延伸 · 阅读

精彩推荐