WangEditor 提交base64格式圖片上傳至服務器


主要新增如圖代碼:

 

 

 附全部代碼如下:

<template lang="html">
  <div class="editor">
    <div ref="toolbar" class="toolbar"></div>
    <div ref="editor" class="text"></div>
  </div>
</template>

<script>
// import E from "wangeditor";
const E = process.browser ? require("wangeditor") : undefined;
export default {
  name:"WangEditor",
  data() {
    return {
      //uploadPath: this.Global.httpUrl_upload_cloud + "upload/image",
      editor: null,
      info_: null
    };
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    isClear: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isClear(val) {
      // 觸發清除文本域內容
      if (val) {
        this.editor.txt.clear();
        this.info_ = null;
      }
    },
    value: function(value) {
      if (value !== this.editor.txt.html()) {
        this.editor.txt.html(this.value);
      }
    }
  },
  mounted() {
    this.seteditor();
    this.editor.txt.html(this.value);
  },
  methods: {
    seteditor() {
      // this.uploadPath = this.Global.httpUrl_upload_cloud + "upload/image";

      // http://192.168.2.125:8080/admin/storage/create
      this.editor = new E(this.$refs.toolbar, this.$refs.editor);
      // this.editor.customConfig.uploadImgServer =
      //   ""; // 配置服務器端地址
      // this.editor.customConfig.uploadImgHeaders = {}; // 自定義 header
      // this.editor.customConfig.uploadFileName = "file"; // 后端接受上傳文件的參數名
      // this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024; // 將圖片大小限制為 2M
      // this.editor.customConfig.uploadImgMaxLength = 6; // 限制一次最多上傳 3 張圖片
      this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000; // 設置超時時間

      // 配置菜單
      this.editor.customConfig.menus = [
        "head", // 標題
        "bold", // 粗體
        "fontSize", // 字號
        "fontName", // 字體
        "italic", // 斜體
        "underline", // 下划線
        "strikeThrough", // 刪除線
        "foreColor", // 文字顏色
        "backColor", // 背景顏色
        "link", // 插入鏈接
        "list", // 列表
        "justify", // 對齊方式
        "quote", // 引用
        "emoticon", // 表情
        "image", // 插入圖片
        "table", // 表格
        "video", // 插入視頻
        "code", // 插入代碼
        "undo", // 撤銷
        "redo", // 重復
        "fullscreen" // 全屏
      ];

      var _this = this;
      this.editor.customConfig.customUploadImg = function(files, insert) {
        // 上傳代碼返回結果之后,將圖片插入到編輯器中
        _this.filesToBase64(files);
      };
      // this.editor.customConfig.uploadImgHooks = {
      //   before: function(xhr, editor, files) {
      //     // 圖片上傳之前觸發
      //     console.log(files);
      //     files=["1111"];
      //     console.log(files);
      //   },
      //   fail: (xhr, editor, result) => {
      //     // 插入圖片失敗回調
      //     this.$toastr.tipError("失敗提示", result);
      //   },
      //   success: (xhr, editor, result) => {
      //     // 圖片上傳成功回調
      //     console.log("圖片上傳成功回調");
      //   },
      //   timeout: (xhr, editor) => {
      //     // 網絡超時的回調
      //     console.log("網絡超時的回調");
      //   },
      //   error: (xhr, editor) => {
      //     // 圖片上傳錯誤的回調
      //     console.log("圖片上傳錯誤的回調");
      //     this.$toastr.tipError("圖片上傳錯誤", "失敗提示");
      //   },
      //   customInsert: (insertImg, result, editor) => {
      //     // 圖片上傳成功,插入圖片的回調
      //     console.log("圖片上傳成功,插入圖片的回調");
      //   }
      // };
      this.editor.customConfig.onchange = html => {
        this.info_ = html; // 綁定當前逐漸地值
        this.$emit("change", this.info_); // 將內容同步到父組件中
      };
      // 創建富文本編輯器
      this.editor.create();
    },
    filesToBase64(files) {
      let _this = this;
      files.map(item => {
        var reader = new FileReader();
        reader.onload = function(e) {
          //_this.uploadImage(e.target.result, item);
          _this.axios
            .post(_this.Global.httpUrl_upload_cloud + "upload/image", {
              data: e.target.result
            })
            .then(res => {
              if (res["result"] == "T") {
                // 插入圖片到editor
                _this.editor.cmd.do(
                  "insertHtml",
                  '<img src="' + res["path"] + '" style="max-width:100%;"/>'
                );
              }
            });
        };
        // 傳入一個參數對象即可得到基於該參數對象的文本內容
        reader.readAsDataURL(item);
      });
    }
  }
};
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM