公司的网站中也要集成富文本编辑器,我们选择了CKEditor 5,官方网址

在集成的过程中需要完成图片上传到公司服务器,在文档中有三种方法,三种方法,

第一种是用CKEditor 的服务肯定是不行,第二种需要后端配置一些东西,最好也不要麻烦别人,第三种就是自己根据引导去写一个自定义插件,因为他都基本写好了,所以比较容易,于是就自己写了一个MyUploadAdapter.js 文件,在需要的地方引用就可以使用其中的init方法来生成一个可以把图片上传到公司服务器的富文本编辑器,代码如下


import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
export default class MyUploadAdapter {constructor( loader, url ) {// The FileLoader instance to use during the upload. It sounds scary but do not// worry — the loader will be passed into the adapter later on in this guide.this.loader = loader;// The upload URL in your server back-end. This is the address the XMLHttpRequest// will send the image data to.this.url = url;}// Starts the upload process.upload() {return new Promise( ( resolve, reject ) => {this._initRequest();this._initListeners( resolve, reject );this._sendRequest();} );}// Aborts the upload process.abort() {if ( this.xhr ) {this.xhr.abort();}}// Initializes the XMLHttpRequest object using the URL passed to the constructor._initRequest() {const xhr = this.xhr = new XMLHttpRequest();// Note that your request may look different. It is up to you and your editor// integration to choose the right communication channel. This example uses// the POST request with JSON as a data structure but your configuration// could be different.xhr.open( 'POST', this.url, true );xhr.responseType = 'json';}// Initializes XMLHttpRequest listeners._initListeners( resolve, reject ) {const xhr = this.xhr;const loader = this.loader;const genericErrorText = 'Couldn\'t upload file:' + ` ${ loader.file.name }.`;xhr.addEventListener( 'error', () => reject( genericErrorText ) );xhr.addEventListener( 'abort', () => reject() );xhr.addEventListener( 'load', () => {const response = xhr.response;// This example assumes the XHR server's "response" object will come with// an "error" which has its own "message" that can be passed to reject()// in the upload promise.//// Your integration may handle upload errors in a different way so make sure// it is done properly. The reject() function must be called when the upload fails.if ( !response || response.error ) {return reject( response && response.error ? response.error.message : genericErrorText );}// If the upload is successful, resolve the upload promise with an object containing// at least the "default" URL, pointing to the image on the server.// This URL will be used to display the image in the content. Learn more in the// UploadAdapter#upload documentation.resolve( {default: response.url} );} );// Upload progress when it is supported. The FileLoader has the #uploadTotal and #uploaded// properties which are used e.g. to display the upload progress bar in the editor// user interface.if ( xhr.upload ) {xhr.upload.addEventListener( 'progress', evt => {if ( evt.lengthComputable ) {loader.uploadTotal = evt.total;loader.uploaded = evt.loaded;}} );}}// Prepares the data and sends the request._sendRequest() {// Prepare the form data.const data = new FormData();data.append( 'File', this.loader.file );// Send the request.this.xhr.send( data );}}export let MyCustomUploadAdapterPlugin = ( editor ) => {editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {// Configure the URL to the upload script in your back-end here!return new MyUploadAdapter( loader, '/common/img/upload' );};
}export let init = () => {ClassicEditor.create( document.querySelector( '#editor'),{toolbar: ["heading", "|", "alignment:left", "alignment:center", "alignment:right", "alignment:adjust", "|", "bold", "italic", "blockQuote", "link", "|", "bulletedList", "numberedList", "imageUpload", "|", "undo", "redo"],extraPlugins: [ MyCustomUploadAdapterPlugin ]} ).then((editor) => {console.log( editor );} ).catch( (error) => {console.error( error );} );
}

调研集成的过程比普通业务代码要复杂一些:

首先是文件的导出导入出了一些问题,

其次是公司的上传接口,即new MyUploadAdapter  处传入的第二个参数,是要首先登陆成功才可以调用,

最后是,_sendRequest方法中的data.append( 'File', this.loader.file );  这里的File 要根据接口定义的变量名来写,

调试过程需要首先弄明白后端的接口需要什么样的数据,根据后端的要求来调整自己的MyUploadAdapter.js

CKEditor5 自定义上传图片相关推荐

  1. ueditor 编辑html文件名,UEditor编辑器自定义上传图片或文件路径的修改方法,ueditor修改方法...

    UEditor编辑器自定义上传图片或文件路径的修改方法,ueditor修改方法 使用ueditor编辑器,附件默认在ueditor/php/upload/,  我的附件地址是网站根目录下/data/u ...

  2. element-ui自定义上传图片

    按照element ui官网的说法,http-request 覆盖默认的上传行为,可以自定义上传的实现. 个人编写的自定义上传图片代码实现如下: html部分: <div class=" ...

  3. vue-quill-editor富文本编辑器自定义上传图片功能

    问题描述:vue-quill-editor富文本默认上传图片是将图片转换成base64进行存储,字段过长:然后进行自定义上传图片,只保存图片路径,减少服务器压力 解决思路: 1.先创建一个上传图片的组 ...

  4. React中:富文本编辑器(react-quill),自定义上传图片到七牛云

    1.组件封装 import React, { useState, useEffect, useRef } from 'react'; import ReactQuill from 'react-qui ...

  5. 储存quill与php,Quill自定义上传图片

    Quill-Demo body { padding: 10px 30px; } #editor { min-height: 180px; } Quill自定义上传图片 /* 编辑器操作条选项 */ v ...

  6. 记录基于elementUI一个自定义上传图片的小功能

    哈喽,这里是今天不写bug的平平,00后程序媛本媛,上班写bug下班改bug的平平(Senior)刚刚进入实习阶段,此次记录一下在项目中遇到的一个小问题,自定义上传图片小功能的一些坑~ 首先先跟我一起 ...

  7. CKeditor自定义上传图片功能

    CKeditor可以配合CKfinder实现文件的上传及管理.但是往往我们上传的图片需要某些自定义的操作,比如将图片路径写入数据库,图片加水印等等操作. 实现原理:配置CKeditor的自定义图标,单 ...

  8. [ckeditor系列]CKeditor自定义上传图片功能

    CKeditor可以配合CKfinder实现文件的上传及管理.但是往往我们上传的图片需要某些自定义的操作,比如将图片路径写入数据库,图片加水印等等操作. 实现原理:配置CKeditor的自定义图标,单 ...

  9. Vue中使用wangEditor实现自定义上传图片和视频

    之前没用过wangEditor真是一脸懵,做自己没做过的东西总是心里没谱,既然项目已经结束了那来总结一下吧 看着官网的demo发现简单了好多 官网地址:快速开始 | wangEditor 这里可以选自 ...

最新文章

  1. [Linux学习]虚拟机系统与实际系统共享文件的方式
  2. Struts2中Action之ResultType
  3. Python正则表达式re模块简明笔记
  4. Linux的命令组成
  5. 音视频技术在云会议场景的拓展和未来
  6. ssl1746-商务旅行【tarjan,LCA】
  7. linux子线程运行的函数_Linux中线程使用详解
  8. 剑指offer 09变态跳台阶
  9. Ubuntu系统下面软件安装更新命令
  10. sqlyog 64位linux版本,linux安装mysql+sqlyog可视化(示例代码)
  11. zotero自动安装word插件失败
  12. UCB CS285课程笔记目录
  13. SpringMvc框架及SSM框架整合
  14. 学生信息管理系统(头哥适用版)(c语言)
  15. c#FileStream文件读写可能会出现乱码
  16. Visual Studio中C++部分的官方文档链接【微软(Microsoft)所有产品的官方文档链接】
  17. 【C】少年时代的经典游戏:三子棋
  18. 记录mumu模拟器AMD SVM(VT)开启失败的问题
  19. AI 与合成生物学「联姻」的五大挑战:技术、数据、算法、评估与社会学
  20. 编曲录音宿主软件-Cubase Elements 11 v11.0.30 WiN 元素版

热门文章

  1. 计算机在F1的应用,电脑入门,F1到F12有什么用
  2. 为什么硬盘总坏?为什么就冠希兄的修好了?
  3. 推荐一款微信 Markdown 编辑器
  4. VMware安装华为存储模拟器
  5. Python获取阿里云产品云监控数据指标
  6. 达摩达兰论估价pdf_《故事与估值:商业故事的价值》pdf文字版下载
  7. 前端人工智能?TensorFlow.js 学会游戏通关
  8. 格凌科技浅谈数据中心的PUE值
  9. matlab生成一个数值一样的向量
  10. 百万级深空天体数据库获取