vue wangeditor上传视频和图片地址配置,粘贴图片上传文件

vue wangeditor上传视频和图片地址配置,粘贴图片上传文件

 

<template>
  <div class="content clearfix">
    <Editor
      style="min-height: 550px; overflow-y: hidden;"
      v-model="content"
      :defaultConfig="editorConfig"
      :mode="modeCss"
      @onCreated="onCreated"
    />
  </div>
</template>
<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
export default {
  components: { Editor, Toolbar },
  data() {
    return {
      editor: null,
      toolbarConfig: {},
      editorConfig: {
        placeholder: "请输入内容...",
        readOnly: false,
        MENU_CONF: {
          //上传参考https://www.wangeditor.com/v5/menu-config.html#%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE
          uploadImage: {
            server: `/api/sys/file/editorUpload`,
            // 超时时间,默认为 10 秒
            timeout: 30 * 1000, // 5s
            fieldName: "file",
            // 将 meta 拼接到 url 参数中,默认 false
            metaWithUrl: false, // join params to url
            // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
            meta: { dataKey: 0, bizType: "common" },
            // 自定义增加 http  header
            // headers: {
            //   Accept: "text/x-json",
            //   otherKey: "xxx"
            // },
            // 跨域是否传递 cookie ,默认为 false
            withCredentials: true,
            // 自定义增加 http  header
            // headers: {
            //   Accept: "text/x-json",
            //   otherKey: "xxx"
            // },
            maxFileSize: 10 * 1024 * 1024, // 10M
            base64LimitSize: 5 * 1024, // insert base64 format, if file's size less than 5kb
            // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
            allowedFileTypes: ["image/*"],
            // 最多可上传几个文件,默认为 100
            maxNumberOfFiles: 30,
            onBeforeUpload(file) {
              console.log("onBeforeUpload", file);

              return file; // will upload this file
              // return false // prevent upload
            },
            onProgress(progress) {
              console.log("onProgress", progress);
            },
            onSuccess(file, res) {
              console.log("onSuccess", file, res);
            },
            onFailed(file, res) {
              alert(res.message);
              console.log("onFailed", file, res);
            },
            onError(file, err, res) {
              alert(err.message);
              console.error("onError", file, err, res);
            }
          },
          uploadVideo: {
            server: "/api/sys/file/editorUpload",
            fieldName: "file",
            // 单个文件的最大体积限制,默认为 10M
            maxFileSize: 5 * 1024 * 1024, // 5M
            // 最多可上传几个文件,默认为 5
            maxNumberOfFiles: 3,
            // 选择文件时的类型限制,默认为 ['video/*'] 。如不想限制,则设置为 []
            allowedFileTypes: ["video/*"],
            // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
            meta: { dataKey: 0, bizType: "video" },
            // 将 meta 拼接到 url 参数中,默认 false
            metaWithUrl: false,
            // 自定义增加 http  header
            // headers: {
            //   Accept: "text/x-json",
            //   otherKey: "xxx"
            // },
            // 跨域是否传递 cookie ,默认为 false
            withCredentials: true,
            // 超时时间,默认为 30 秒
            timeout: 30 * 1000, // 15 秒
            // 上传进度的回调函数
            onProgress(progress) {
              console.log("progress", progress);
            },
            // 单个文件上传成功之后
            onSuccess(file, res) {
              console.log(`${file.name} 上传成功`, res);
            },
            // 单个文件上传失败
            onFailed(file, res) {
              console.log(`${file.name} 上传失败`, res);
            },
            // 上传错误,或者触发 timeout 超时
            onError(file, err, res) {
              console.log(`${file.name} 上传出错`, err, res);
            }
          }
        }
      },
      modeCss: "default" // or 'simple'
    };
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错
      // 带格式粘贴
      this.editor.customConfig.pasteFilterStyle = false;
      //忽略粘贴内容中的图片
      this.editor.customConfig.pasteIgnoreImg = false;
      this.editor.customConfig.customUploadImg = (files, insert) => {
        // 图片自定义上传方法
        this.uploadFile(files[0]);
      };
      this.editor.customConfig.onchange = html => {
        this.info_ = html; // 绑定当前值
        this.$emit("change", this.info_); // 将内容同步到父组件中
      };
    },
    uploadFile(file) {
      this.$http
        .postData("/api/sys/file/editorUpload", {
          dataKey: 0,
          bizType: "common",
          file: file
        })
        .then(res => {
          if (res && res.success) {
            this.$success("上传成功");
          } else {
            this.$error(res.message || "操作失败!");
          }
        });
    }
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时,及时销毁编辑器
  }
};
</script>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值