参考:https://blog.csdn.net/kevinfan2011/article/details/91047062
TinyMCE提供了图片异步上传处理函数images_upload_handler让用户配置上传图片的相关参数,这里有三个参数,图片数据(blobinfo是一个对象,包含上传文件的信息),成功时的回调函数(success,上传成功的时候向success传入一个图片地址),失败时的回调函数(failure,失败的时候向 failure 传入报错信息),这里我们可以通过images_upload_handler来重新自定义一个上传方法以便适配我们的项目。
1.配置插件和toolbar
plugins:
"link lists image code table colorpicker textcolor wordcount contextmenu imageSelector",
toolbar:
"bold italic underline strikethrough | fontsizeselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent blockquote | undo redo | lists image media table imageupload | removeformat ",
2.设置handler
images_upload_handler:(blobInfo, success, failure) =>{
this.handleImgUpload(blobInfo, success, failure)
}
- 设置回调函数
handleImgUpload (blobInfo, success, failure) {
let formdata = new FormData()
formdata.set('file',blobInfo.blob())
uploadShareFile(formdata).then(
res=>{
console.log(res)
if(res.code == 1){
success(res.data.path)
}else{
failure('error')
}
}
).catch(res=>{
failure('error')
})
}