react quill中圖片上傳由默認轉(zhuǎn)成base64改成上傳到服務器的方法
更新時間:2019年10月30日 10:12:56 作者:all2005
這篇文章主要介紹了react quill中圖片上傳由默認轉(zhuǎn)成base64改成上傳到服務器的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
使用react-quill富文本編輯器,里面處理圖片是默認轉(zhuǎn)成base64,提交到后臺的時候文件太大,因此這里改寫處理image的邏輯,改成上傳到服務器。
具體代碼如下:
配置1
import Quill from 'quill'
import ReactQuill from 'react-quill'
import 'react-quill/dist/quill.core.css'
import 'react-quill/dist/quill.snow.css'
import QuillEmoji from 'quill-emoji'
import 'quill-emoji/dist/quill-emoji.css'
Quill.register({
'modules/emoji-toolbar': QuillEmoji.ToolbarEmoji,
// 'modules/emoji-textarea': QuillEmoji.TextAreaEmoji,
'modules/emoji-shortname': QuillEmoji.ShortNameEmoji
})
const toolbarContainer = [
[{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
[{ 'font': [] }],
[{ 'header': 1 }, { 'header': 2 }], // custom button values
['bold', 'italic', 'underline', 'strike'], // toggled buttons
[{ 'align': [] }],
[{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent
[{ 'direction': 'rtl' }], // text direction
[{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript
['blockquote', 'code-block'],
[{ 'list': 'ordered' }, { 'list': 'bullet' }],
[{ 'color': [] }, { 'background': [] }],
['emoji', 'image', 'video', 'link'],
['clean']
]
配置2
<ReactQuill
ref={ref => this.quillRef = ref}
placeholder="填寫活動詳情~"
theme="snow"
value={this.state.detailTpl}
onChange={this.handleChangeDetail}
modules={{
toolbar: {
container: toolbarContainer,
handlers: {
image: this.imageHandler
}
},
'emoji-toolbar': true,
// 'emoji-textarea': true,
'emoji-shortname': true,
}}
/>
圖片處理方法
imageHandler = () => {
this.quillEditor = this.quillRef.getEditor()
const input = document.createElement('input')
input.setAttribute('type', 'file')
input.setAttribute('accept', 'image/*')
input.click()
input.onchange = async () => {
const file = input.files[0]
const formData = new FormData()
formData.append('quill-image', file)
const res = await uploadFile(formData)
const range = this.quillEditor.getSelection()
const link = res.data[0].url
// this part the image is inserted
// by 'image' option below, you just have to put src(link) of img here.
this.quillEditor.insertEmbed(range.index, 'image', link)
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
React?Hook中的useState函數(shù)的詳細解析
Hook 就是 JavaScript 函數(shù),這個函數(shù)可以幫助你鉤入(hook into) React State以及生命周期等特性,這篇文章主要介紹了React?Hook?useState函數(shù)的詳細解析的相關資料,需要的朋友可以參考下2022-10-10

