引言
wangEditor本身是不支持本地視頻上傳的,但是支持配置亩歹,通過(guò)配置進(jìn)行修改
操作
代碼如下
seteditor () {
// this.editor = new E(this.$refs.editor)
this.editor.config.uploadImgShowBase64 = false // base 64 存儲(chǔ)圖片
this.editor.config.uploadImgServer = 'api/oss/uploadImages'// 填寫配置服務(wù)器端地址
this.editor.config.uploadImgHeaders = { 'token': this.token }// 自定義 header
this.editor.config.uploadFileName = 'file' // 后端接受上傳文件的參數(shù)名
this.editor.config.uploadImgMaxSize = 8 * 1024 * 1024 // 將圖片大小限制為 2M
this.editor.config.uploadImgMaxLength = 6 // 限制一次最多上傳 6 張圖片
this.editor.config.uploadImgTimeout = 3 * 60 * 1000 // 設(shè)置超時(shí)時(shí)間
// 自定義 onchange 觸發(fā)的延遲時(shí)間书幕,默認(rèn)為 200 ms
this.editor.config.onchangeTimeout = 1000 // 單位 ms
this.editor.config.onchange = (html) => {
this.info_ = html // 綁定當(dāng)前逐漸地值
this.$emit('change', this.info_) // 將內(nèi)容同步到父組件中
}
// 視頻上傳
this.editor.config.uploadVideoServer = 'api/oss/uploadFileVideo' // 上傳接口
this.editor.config.uploadVideoParams = {
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundarycZm1pHksXeHS6t5r'
}
// this.editor.config.uploadVideoParams = {
// 'Content-Type': 'multipart/form-data'
// }
this.editor.config.uploadVideoHeaders = { 'token': this.token }// 自定義 header
this.editor.config.uploadVideoName = 'file'
this.editor.config.uploadVideoHooks = {
// 上傳完成處理方法
fail: (xhr, editor, result) => {
// 插入圖片失敗回調(diào)
},
success: (xhr, editor, result) => {
// 圖片上傳成功回調(diào)
},
timeout: (xhr, editor) => {
// 網(wǎng)絡(luò)超時(shí)的回調(diào)
},
error: (xhr, editor) => {
// 圖片上傳錯(cuò)誤的回調(diào)
console.log('err--->', xhr)
},
customInsert: function (insertVideo, result) {
console.log('result--->', result)
if (result.msg === '上傳成功') {
const link = result.url
insertVideo(link)
// (result.data || '').split(',').forEach(function (link) {
// link && insertVideo(link)
// })
} else {
console.log('result--->', result)
flavrShowByTime('上傳失敗', null, 'danger')
}
}
}
// 創(chuàng)建富文本編輯器
this.editor.create()
this.editor.config.uploadImgHooks = {
fail: (xhr, editor, result) => {
// 插入圖片失敗回調(diào)
},
success: (xhr, editor, result) => {
// 圖片上傳成功回調(diào)
},
timeout: (xhr, editor) => {
// 網(wǎng)絡(luò)超時(shí)的回調(diào)
},
error: (xhr, editor) => {
// 圖片上傳錯(cuò)誤的回調(diào)
},
customInsert: (insertImg, result, editor) => {
// 循環(huán)插入圖片
let url = result.url
insertImg(url)
}
}
},
視頻上傳和圖片上傳不一樣的地方在于代碼中這一句
this.editor.config.uploadVideoName = 'file'
沒有這一句在一切配置都正確的情況下新荤,調(diào)用上傳視頻的接口會(huì)報(bào)錯(cuò)誤500,這是因?yàn)?img class="math-inline" src="https://math.jianshu.com/math?formula=%5Ccolor%7Bred%7D%7BwangEditor%E7%9A%84%E9%BB%98%E8%AE%A4%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E7%B1%BB%E5%9E%8B%E5%B9%B6%E4%B8%8D%E6%98%AFfile%EF%BC%8C%E4%BD%86%E6%98%AF%E4%B8%80%E8%88%AC%E5%90%8E%E7%AB%AF%E9%AA%8C%E8%AF%81%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B%E9%BB%98%E8%AE%A4%E6%98%AFfile%7D" alt="\color{red}{wangEditor的默認(rèn)文件上傳類型并不是file迟隅,但是一般后端驗(yàn)證的文件類型默認(rèn)是file}" mathimg="1">
代碼中的 insertVideo(link)和insertImg(url)是wangEditor自帶的方法励七,參數(shù)是后端返回的URL