ckEditor5踩坑(2)

1.上传图片(使用自定义上传适配器的js文件)

import {
  storage,
  createToken
} from '@/api/management.js'
import { Message } from 'element-ui'
import axios from 'axios'

export default class MyUploadAdapter {
  setApiToken(v) {
    this._apiToken = v
  }
  getApiToken() {
    return this._apiToken
  }

  constructor(loader, editor) {
    this.loader = loader
    this.editor = editor
    this.oldValue = editor.getData()
  }

  async upload() {
    let ls = await this._initRequest()
    console.log(ls)
    return ls
  }
  // Aborts the upload process.
  abort() {
    // console.log(cancel)
    let token = this.getApiToken()
    this.cancel = true
    if (token) {
      console.log('取消上传', token)
      token.cancel('取消上传')
    }
    // if (typeof cancel === 'function') {
    //   // console.log(88)
    //   // 取消请求
    //   let res = cancel()
    //   console.log(res)
    // }
  }
  // Initializes the XMLHttpRequest object using the URL passed to the constructor.
  _initRequest() {
    return new Promise(async (resolve, reject) => {
      this.cancel = false
      let fileInfo = {}
      fileInfo = await this.loader.file
      // 限制格式
      const fileSuffix = fileInfo.name.substring(fileInfo.name.lastIndexOf('.') + 1)
      /* eslint-disable */
      if (['jpg','png','jpeg'].indexOf(fileSuffix) === -1) {
        Message({
          message: '上传格式错误,应为jpg,png,jpeg',
          type: 'warning'
        })
        this.editor.setData(this.oldValue)
        return false
      }
      /* eslint-enable */
      // 限制大小
      const tooLong = fileInfo.size / 1024 > 2048
      if (tooLong) {
        Message({
          message: '上传文件大小不能超过 ' + (2048 / 1024) + 'MB!',
          type: 'warning'
        })
        this.editor.setData(this.oldValue)
        return false
      }
      let fileInputObj = {}
      fileInputObj.bucket = 'img'
      fileInputObj.name = fileInfo.name
      fileInputObj.size = fileInfo.size
      fileInputObj.userName = ''
      fileInputObj.password = ''
      let res = await createToken(fileInputObj)
      if (res.data.code === 200 && !this.cancel) {
        let uploadToken = res.data.msg
        const fileObj = fileInfo
        // // FormData 对象
        const form = new FormData()
        // // 文件对象
        form.append('files', fileObj)
        let apiToken = axios.CancelToken.source()
        this.setApiToken(apiToken)
        console.log('upload', apiToken)
        storage(form, {
          ossToken: uploadToken
        }, apiToken.token).then(res => {
          if (res.data.code === 200) {
            let ossUrl = process.env.VUE_APP_HTTP_OSS.split(':')
            resolve({
              default: ossUrl[0] + ':' + ossUrl[1] + ':8329/upload/' + res.data.msg
            })
          }
        }).catch(function(thrown) {
          console.log(333, thrown)
          Message({
            message: '上传已取消',
            type: 'error'
          })
          // reject(new Error())
          // reject('出错了')
          // if (axios.isCancel(thrown)) {
          //   console.log('Request canceled', thrown.message)
          // } else {
          //   // handle error
          // }
        })
      }
    })
  }
}

export let MyCustomUploadAdapterPlugin = (editor) => {
  editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
    return new MyUploadAdapter(loader, editor)
  }
}

2.使用适配器

import { MyCustomUploadAdapterPlugin } from 'ckUploadAdapter.js'
const config = {
  extraPlugins: [MyCustomUploadAdapterPlugin]
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 矿工人纯扑实在,无论做什么事情,从来也不去费脑子想,去讲究一番,也不去分个场合情况,想干就干,想做就做,就象吃家常...
    漠北寒鹰阅读 226评论 0 0
  • 若教西子入雨霖,濡发鲛泪染白绫。 何故李下成蹊吝,前毁今饰非人怨。 对诗 原文: 欲画泰山一青松,苍翠横虬凌岱顶。...
    喵咪薄荷糖喵阅读 241评论 0 3
  • 妈妈 听到这两个字 每次心里头总会别地一跳 像是做错了什么事 被妈妈拧了耳朵 很多年了,妈妈 我一直想送你五个字 ...
    断红尘阅读 75评论 0 0
  • 我羡慕那些整天在空间秀自己学校的同学。呵呵,他们的大学生活真是丰富精彩啊。不像我,我厌恶周围的一切,感觉自己就像进...
    王宝玉啊阅读 1,118评论 0 2
  • 今天早上起床的时候纠结了半天才起床去跑步。最终还是我想要健康的身体战胜了我的懒惰。但其实还是有个更快速起床和战...
    无誨阅读 142评论 0 1

友情链接更多精彩内容