最近在开发一个微信公众号的后台,微信公众号编辑的文章一直没有得到很好地适应,大多数人也是在其他的编辑软件中编辑好之后直接去复制到微信公众平台中,考虑到复制后会排版出现问题,所以给大家推荐一款很不错的Web富文本编辑器 (summernote)

官网地址为: https://summernote.org/

首先看一下编辑器的样子:

接下来就简单的实现这个编辑器。

1.首先需要从官网下载summernote的包(js和css两部分)然后新建一个页面,因为这个是建立在jquery和bootstarp两个基础上的,所以我们也需要引用他们,以下为我们引入的五个文件,代码如下:

  <script src="../../js/jquery-3.1.1.min.js"></script>
<link href="../../css/bootstrap.css" rel="stylesheet" />
<script src="../../js/bootstrap.min.js"></script>
<link href="dist/summernote.css" rel="stylesheet" />
<script src="dist/summernote.js"></script>

2.创建一个summernote的容器。

 <div id="summernote">Hello Summernote</div>

3.创建summernote,并且将上传的图片保存到本地,然后再返回到编辑器中,如果直接复制图片不去保存,图片的地址会是原来的地址,并不是本地的地址。所以我们需要将图片保存到本地,然后就可以将文章获取存入数据库了。

    <script>
$(document).ready(function () {
$('#summernote').summernote({
height: ,
width:,
minHeight: ,
maxwidth: ,
minwidth: ,
maxHeight: ,
focus: false,
callbacks: {
onImageUpload: function (files, editor) {
var $files = $(files);
// 通过each方法遍历每一个file
$files.each(function () {
var file = this;
// FormData,新的form表单封装,具体可百度,但其实用法很简单,如下
var data = new FormData();
// 将文件加入到file中,后端可获得到参数名为“file”
data.append("file", file);
// ajax上传
$.ajax({
data: data,
type: "POST",
url: "../../ashx_html/summernote.ashx",// div上的action
cache: false,
contentType: false,
processData: false,
// 成功时调用方法,后端返回json数据
success: function (response) {
console.log(response)
var json = $.parseJSON(response)
if (json.state == "error") {
alert("上传失败")
} else {
// 插入到summernote
$('#summernote').summernote('insertImage', json.img_url);
}
},
});
})
}
}
});
});
</script>

4.现在我们就可以在页面上看到编辑器了,现在需要建立一个后台接收器,来接受上传的图片保存,然后将图片地址返回到页面。这里我使用ashx来接受,也可以用其他的方式,逻辑都是一样的。

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpFileCollection file_ = context.Request.Files;
sumnmer_data summer_list = new sumnmer_data();
if (file_.Count!=)
{
long size = file_[].ContentLength;//文件大小
string type = file_[].ContentType;//文件类型
string name = file_[].FileName;//文件名
string _tp = System.IO.Path.GetExtension(name);//文件后缀名
string saveName = DateTime.Now.ToString("yyyyMMddHHmmssfff");//保存后的文件名称(这里使用时间戳)
string file = "\\admin\\summer_img\\"; //保存的路径
string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + file;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
DirectoryInfo dirInfo = new DirectoryInfo(path);
dirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
}
file_[].SaveAs(path + saveName + _tp);
summer_list.state = "succes";
summer_list.img_url = file + saveName + _tp;
}
else
{
summer_list.state = "error";
}
context.Response.Write(JsonConvert.SerializeObject(summer_list)); }
     //返回的类
public class sumnmer_data {
public string state { get; set; } //(成功为succes,失败为error)
public string img_url { get; set; }//图片存到本地的路径
}

     将文件获取 按路径保存,将保存图片的地址和状态 存入summer_data中 ,然后转为json字符串返回到页面

5.获取编辑器的内容:

 var markupStr = $('#summernote').summernote('code');
console.log(markupStr) // 打印到控制器

以上就是summernote富文本编辑器的操作和用法,视频上传和这个类似,还有一些编辑器控件的添加和删除,还有一些编辑器的样式修改 官网上都有很详细的说明,不明白的可以留言,我努力解答。

summernote富文本编辑器的使用的更多相关文章

  1. summernote富文本编辑器

    下载summernote官方demo,解压,把文件夹中的summernote.js,summernote.css和font整个文件夹都放到服务器对应的项目目录里 引入summernote 所需要的bo ...

  2. summernote富文本编辑器配合validate表单验证无法进行表单提交的问题

    1.使用summernote富文本编辑器提交图片到服务器 在使用bootstrap中,我们用到了summernote富文本编辑器,使用summernote将图片上传到服务器中,参考我的上篇文章http ...

  3. SummerNote 富文本编辑器 - Rails 集成

    使用官方提供的CDN服务 将下面一段加入layout文件中 <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css ...

  4. summernote 富文本编辑器限制输入字符长度

    项目中需要一个比较简单的富文本编辑器,于是选中了summernote .虽然比较轻量,但是在开发中也遇到了几个问题,在此记录一下. 1:样式和bootstrap冲突,初始化之后显示为: .note-e ...

  5. summernote(富文本编辑器)将附件与图片上传到自己的服务器(vue项目)

    1.上传图片至自己的服务器(这个官方都有例子,重点介绍附件上传)图片上传官方网址 // onChange callback $('#summernote').summernote({ callback ...

  6. 【实践】简洁大方的summernote 富文本编辑器插件的用发——小技巧

    前面说到summernote 的上传,可是我要知道怎么获取内容呀,很简单调用一下函数便可: 获取内容: $("#user-work-content").summernote(&qu ...

  7. 超好用的富文本编辑器Summernote的使用

    官网地址 中文文档 源码下载地址 Summernote依赖于jquery和bootstrap3/4   所以用时记得引入这俩依赖 奉上引入方法(官网说的很清楚,api也很详细): <!-- in ...

  8. 在 Vue 项目中引入 tinymce 富文本编辑器

    项目中原本使用的富文本编辑器是 wangEditor,这是一个很轻量.简洁编辑器 但是公司的业务升级,想要一个功能更全面的编辑器,我找了好久,目前常见的编辑器有这些: UEditor:百度前端的开源项 ...

  9. vue中引入Tinymce富文本编辑器

    最近想在项目上引入一个富文本编辑器,之前引入过summernote,感觉并不太适合vue使用, 然后在网上查了查,vue中使用Tinymce比较适合, 首先,我们在vue项目的components文件 ...

随机推荐

  1. Flume -- Transfer one type of source to another type

    Source within Flume is a kind of Server for outside client. Sink within Flume is a kind of client fo ...

  2. 安装 Python IDLE (Linux)

    Python IDLE (Integrated Development and Learning Environment) 是一个官方的轻量级 Python IDE.在不同的 Linux 的发布版本中 ...

  3. java线程操作

    目录 前言 创建多线程的方式 1继承thread抽象类 2实现Runnable接口 3实现Callable接口 匿名内部类 线程池 线程安全 同步代码块 同步方法 锁机制 线程状态 前言 进程:内存运 ...

  4. Linux下postgres安装fuzzystrmatch其他拓展包

    (1)安装gdal # wget http://download.osgeo.org/gdal/2.0.0/gdal-2.0.0.tar.gz # tar zxvf gdal-2.0.0.tar.gz ...

  5. Python学习---重点模块的学习【all】

    time     [时间模块] import time # print(help(time)) # time模块的帮助 print(time.time()) # 时间戳 print(time.cloc ...

  6. linux gcc 区分32位或64位编译 && 请问arm存储,是以小端格式还是以大端格式?

    linux gcc 区分32位或64位编译   Linux系统下程序如何区分是64位系统还是32位系统 经过对include的翻查,最后确定gcc以__i386__来 进行32位编码,而以__x86_ ...

  7. Undirected Graphs

    无向图 Introduction 图是由边连接的点的集合,有着广泛的应用空间. 一些图的术语,点,边,路径,环(圈),连通分量(子图). 简单路径不重复经过点,简单环不含有重复点和边,简单图不含自环和 ...

  8. Chapter 4 Left Outer Join in MapReduce

    4.1 Introdution Consider a company such as Amazon, which has over 200 millions of users and possibly ...

  9. 第一次课堂作业之Circle

    1.问题描述: Create a program that asks for the radius of a circle and prints the area of that circle, us ...

  10. python29 excel写模块xlwt

    xlwt模块用于新建excel文件并写入数据. 安装 pip install xlwt 简单使用 import xlwt from datetime import datetime #样式 style ...