Vue使用tinymce富文本编辑器组件 实现多附件上传、批量上传

场景:

由于维护老项目 用的tinymce富文本编辑器,运营人员觉得单个上传附件太复杂了于是提出了 多附件上传的优化

查看文档发现 tinymce插件本身并不支持选择多附件上传,但是他会有拓展组件支持此功能。

再次放下拓展组件传送门ax多图片批量上传插件 | TinyMCE中文文档中文手册

实现:

最下方会给出完整使用代码

1.资源配置

首先根据上面链接下载相应的文件axupimgs.zip

解压后可以将upfiles.html放在public目录,js文件可以放在assets文件夹中(目录名无所谓只要引入正确就可以)

由于我们移动了html文件的位置 因此也需要需改一下响应plugin.js的引入路径 见下图  (代码实现非常简单,有能力的可以阅读下代码 修改想要的其他需求)

2.代码实现

如何使用这个拓展插件

首先在使用的页面进行引用上方的plugin.js

import "@/assets/js/plugins/axupimgs/plugin";

初始化配置富文本配置

以下为完整配置:其实主要的改动就是

toolbar里添加axupimgs

plugins里添加axupimgs

     setting: {
        menubar: false,
        toolbar:
          "undo redo | fullscreen | formatselect alignleft aligncenter alignright alignjustify | image axupimgs table | numlist bullist | link unlink | fontselect fontsizeselect forecolor backcolor | bold italic underline strikethrough | indent outdent | superscript subscript | removeformat |",
        plugins: "link image  table lists fullscreen axupimgs",
        language: "zh_CN",
        height: 350,
        images_upload_url: true,
        images_upload_handler: function (blobInfo, succFun, failFun) {
          var formData = new FormData();
          let url = "/common/qiniu/upload";
          formData.append("file", blobInfo.blob()); //此处与源文档不一样

          axios
            .post(url, formData, {
              headers: signActionTime("post", formData),
            })
            .then((response) => {
              console.log(response);
              succFun(response.data.result.url);
            })
            .catch((error) => {
              failFun(error);
            });
        },
      },
3.完整代码
<template>
  <div class="company_add">
    <vue-tinymce v-model="ruleForm.content" :setting="setting" />
  </div>
</template>

<script>
import constant from "constant";
import { ERR_OK } from "constant/statusCode";// 请求状态吗200
import axios from "axios";
import { signActionTime } from "util/js/headerToken";  //为上传服务器的请求头 无需在意
import "@/assets/js/plugins/axupimgs/plugin";
export default {
  data() {
    return {
      uploadUrl: constant.GLOBAL_CONFIG.UPLOADIMG_Url,
      myHeaders: {},
      ruleForm: {
        content: "", //内容
      },
      setting: {
        menubar: false,
        toolbar:
          "undo redo | fullscreen | formatselect alignleft aligncenter alignright alignjustify | image axupimgs table | numlist bullist | link unlink | fontselect fontsizeselect forecolor backcolor | bold italic underline strikethrough | indent outdent | superscript subscript | removeformat |",
        plugins: "link image  table lists fullscreen axupimgs",
        language: "zh_CN",
        height: 350,
        images_upload_url: true,
        images_upload_handler: function (blobInfo, succFun, failFun) {
          var formData = new FormData();
          let url = "/common/qiniu/upload";
          formData.append("file", blobInfo.blob()); //此处与源文档不一样

          axios
            .post(url, formData, {
              headers: signActionTime("post", formData),
            })
            .then((response) => {
              console.log(response);
              succFun(response.data.result.url);
            })
            .catch((error) => {
              failFun(error);
            });
        },
      },
    };
  },
};
</script>

<style lang="scss" scoped></style>

Vue2 Tinymce富文本编辑器是一种用于在Vue2项目中实现富文本编辑功能的插件。您可以按照以下步骤进行安装和使用: 1. 首先,您需要安装依赖。可以通过在终端中运行以下命令来安装依赖: ``` npm install tinymce ``` 2. 接下来,您需要将tinymce的skins文件夹复制到您的项目中。您可以在node_modules/tinymce目录下找到skins文件夹,并将其复制到您的src/assets/tinymce目录下。 3. 然后,您可以创建一个Tinymce.vue组件来封装Tinymce编辑器。可以根据您的需求进行自定义配置,***并将其放置在您的项目中。 5. 在Vue组件使用Tinymce编辑器时,您可以直接导入Tinymce组件并在template中使用它。您可以根据需要通过props传递参数给Tinymce组件。 6. 最后,在整体的目录结构中,您需要确保Tinymce相关的文件和依赖正确地放置在对应的位置。 在使用Vue2 Tinymce富文本编辑器的过程中,您可能会遇到一些问题,比如路径找不到导致无法引入“tinymce/icons/default”的问题。这时,您可以尝试升级tinymce的版本来解决这个问题。 希望以上信息对您有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【vuevue2 中使用 Tinymce 富文本编辑器](https://blog.csdn.net/qq_46123200/article/details/130099360)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值