Quill 剪贴板粘贴base64图片转上传

直接上代码:

base64ToFile (base64Data, filename) {
      // 将base64的数据部分提取出来
      const parts = base64Data.split(';base64,');
      const contentType = parts[0].split(':')[1];
      const raw = window.atob(parts[1]);

      // 将原始数据转换为Uint8Array
      const rawLength = raw.length;
      const uInt8Array = new Uint8Array(rawLength);
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
      }

      // 使用Blob创建File对象
      const blob = new Blob([uInt8Array], { type: contentType });
      return new File([blob], filename, { type: contentType });
    }
this.Quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, Delta) => {
        if (node.tagName === "IMG" && node.src.indexOf("data:image/png;base64,") != -1) {
          const file = this.base64ToFile(node.src, 'image.png')
          upload({ file, filename: 'file' }).then(res => {
            if (res.code == 200) {
              document.querySelector('img[src="' + node.src + '"]').src = res.fileName
            } else {
              Delta.ops = [];
              this.$message.error("图片上传失败:" + res.msg);
              this.$message.warning("消息內容不支持图片!");
            }
          });
        }
        return Delta;
      });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容