富文本编辑器在react中的使用----自定义图片上传

开发背景
react + antd + wangeditor
开发流程
安装富文本编辑器
npm i wangeditor --save
页面中初始化编辑器容器
componentDidMount() {
	this.initEditor ()
}

// 初始化编辑器
initEditor = (content) => {
	const editor = new E('#editor')  // jsx中创建id为editor的div容器
	editor.config.height = 160 // 设置高度

	// 通过base64显示图片
	// editor.config.uploadImgShowBase64 = true

	// 自定义上传图片到后端返回url, 插入到编辑器中
	editor.config.customUploadImg = function (resultFiles, insertImgFn) {
	
		const formData = new FormData();
		// 组装接口需要的字段,根据自己后端需要的字段传哦~
		formData.append('name', resultFiles[0]['name'])
		formData.append('type', resultFiles[0]['type'])
		formData.append('lastModifiedDate', resultFiles[0]['lastModifiedDate'])
		formData.append('size', resultFiles[0]['size'])
		formData.append('upfile', resultFiles[0])
		
		// 调用后端上传图片接口,返回url,
		ueditorUploadImage(formData).then(res => {
		//  通过insertImgFn插入富文本编辑器中
			if (res.url) insertImgFn(res.url)
		})
	}

	// 创建并在页面中渲染编辑器
	editor.create()

	// 追加内容,放在create之后
	if (content) editor.txt.append(content)
}
展示效果

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值