wangEditor配合腾讯cos实现图片上传

项目中遇到wangEditor配合腾讯cos实现图片上传的问题,查了一圈,大家写的方法都不能解决,最终自己试了出来,记录一下。

1.配置cos

此部分详见cos官网,不再赘述;

2.引入cos

import COS from 'cos-js-sdk-v5'
//配置id和密钥
const cos = new COS({
    SecretId:'xxxx',
    SecretKey:'xxxxxxxx'
})

3.引入Editor

详见官网

4.在 onCreated中更改配置

    onCreated(editor) {
      // 一定要用 Object.seal() ,否则会报错
      this.editor = Object.seal(editor); 
     
      //图片配置
      // 自定义上传方法
      const customUpload = (file, insertFn) => {
        // file 即选中的文件
        // 自己实现上传,并得到图片 url alt href
        // 最后插入图片
        // const url = this.uploadImg(file);
        cos.putObject(
          {
            Bucket: "xxxxx", // 存储桶名称
            Region: "ap-chengdu", // 地域
            Key: file.name, // 文件名
            Body: file, // 要上传的文件对象
            StorageClass: "STANDARD", // 上传的模式类型 直接默认 标准模式即可
          },
          (err, data) => {
            if (data?.Location) {
              const url = "http://" + data.Location;
              insertFn(url);
            }
          }
        );
      };
      //将定义的上传方法与绑定给editor的uploadImage配置
      editor.getMenuConfig("uploadImage").customUpload = customUpload;
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liyfn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值