如何将React Quill中图片上传从默认转为上传至服务器而非转换为base64?
- 内容介绍
- 文章标签
- 相关推荐
本文共计503个文字,预计阅读时间需要3分钟。
使用react-quill富文本编辑器,处理图片时默认将图片转换为base64。提交到后台时,文件过大。为此,修改处理image的逻辑,改为上传到服务器。代码如下:
javascriptimport Quill from 'quill';
// 创建Quill实例const editor=new Quill('#editor', { theme: 'snow'});
// 监听图片上传事件editor.on('image', (range, src)=> { // 创建FormData对象 const formData=new FormData(); formData.append('image', src);
// 发送POST请求到服务器 fetch('/upload', { method: 'POST', body: formData }) .then(response=> response.json()) .then(data=> { // 替换图片src为服务器返回的URL editor.formatText(range.index, range.length, 'image', data.url); }) .catch(error=> console.error('上传图片失败:', error));});
使用react-quill富文本编辑器,里面处理图片是默认转成base64,提交到后台的时候文件太大,因此这里改写处理image的逻辑,改成上传到服务器。
本文共计503个文字,预计阅读时间需要3分钟。
使用react-quill富文本编辑器,处理图片时默认将图片转换为base64。提交到后台时,文件过大。为此,修改处理image的逻辑,改为上传到服务器。代码如下:
javascriptimport Quill from 'quill';
// 创建Quill实例const editor=new Quill('#editor', { theme: 'snow'});
// 监听图片上传事件editor.on('image', (range, src)=> { // 创建FormData对象 const formData=new FormData(); formData.append('image', src);
// 发送POST请求到服务器 fetch('/upload', { method: 'POST', body: formData }) .then(response=> response.json()) .then(data=> { // 替换图片src为服务器返回的URL editor.formatText(range.index, range.length, 'image', data.url); }) .catch(error=> console.error('上传图片失败:', error));});
使用react-quill富文本编辑器,里面处理图片是默认转成base64,提交到后台的时候文件太大,因此这里改写处理image的逻辑,改成上传到服务器。

