wangEditor上传本地视频

引言

wangEditor本身是不支持本地视频上传的,但是支持配置,通过配置进行修改

操作

代码如下

seteditor () {
      // this.editor = new E(this.$refs.editor)
      this.editor.config.uploadImgShowBase64 = false // base 64 存储图片
      this.editor.config.uploadImgServer = 'api/oss/uploadImages'// 填写配置服务器端地址
      this.editor.config.uploadImgHeaders = { 'token': this.token }// 自定义 header
      this.editor.config.uploadFileName = 'file' // 后端接受上传文件的参数名
      this.editor.config.uploadImgMaxSize = 8 * 1024 * 1024 // 将图片大小限制为 2M
      this.editor.config.uploadImgMaxLength = 6 // 限制一次最多上传 6 张图片
      this.editor.config.uploadImgTimeout = 3 * 60 * 1000 // 设置超时时间
      // 自定义 onchange 触发的延迟时间,默认为 200 ms
      this.editor.config.onchangeTimeout = 1000 // 单位 ms

      this.editor.config.onchange = (html) => {
        this.info_ = html // 绑定当前逐渐地值
        this.$emit('change', this.info_) // 将内容同步到父组件中
      }

      // 视频上传
      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) => {
          // 插入图片失败回调
        },
        success: (xhr, editor, result) => {
          // 图片上传成功回调
        },
        timeout: (xhr, editor) => {
          // 网络超时的回调
        },
        error: (xhr, editor) => {
          // 图片上传错误的回调
          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')
          }
        }
      }

      // 创建富文本编辑器
      this.editor.create()

      this.editor.config.uploadImgHooks = {
        fail: (xhr, editor, result) => {
          // 插入图片失败回调
        },
        success: (xhr, editor, result) => {
          // 图片上传成功回调
        },
        timeout: (xhr, editor) => {
          // 网络超时的回调
        },
        error: (xhr, editor) => {
          // 图片上传错误的回调
        },
        customInsert: (insertImg, result, editor) => {
          // 循环插入图片
          let url = result.url
          insertImg(url)
        }
      }
    },

视频上传和图片上传不一样的地方在于代码中这一句

this.editor.config.uploadVideoName = 'file'

没有这一句在一切配置都正确的情况下,调用上传视频的接口会报错误500,这是因为\color{red}{wangEditor的默认文件上传类型并不是file,但是一般后端验证的文件类型默认是file}

代码中的 insertVideo(link)和insertImg(url)是wangEditor自带的方法,参数是后端返回的URL

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

相关阅读更多精彩内容

友情链接更多精彩内容