Vue集成mavon-editor实现图片上传

Vue集成mavon-editor实现图片上传

项目环境:
前端:vue2
后端:springboot

前端

1. 安装mavon-editor
npm install mavon-editor@2.10.1
2. 在main.js引入
// ......
import Vue from 'vue'
import mavonEditor from "mavon-editor";
import "mavon-editor/dist/css/index.css"
// ......
Vue.use(mavonEditor)
3. 在vue页面中使用该组件

(1)@imgAdd方法监听图片上传,可以获取到对应的图片文件。其中使用this.$refs可以获得mavon-editor的实例(例如下面代码中<mavon-editor>标签中refmd,那么可以通过this.$refs.md得到mavon-editor实例)

(2)@change方法监听文本框中内容的变化,可以获取文本内容

<mavon-editor
    v-model="content"
    ref="md"
    @imgAdd="imgAdd"
    @change="change"
/>
// uploadImg为上传图片的接口
import { uploadImg } from "@/api/image";

export default {
  data: function() {
    return {
      content: "",
      html: "",
    };
  },
  methods: {
    imgAdd(pos, $file) {
      // pos为位置,$file为图片文件
      let formdata = new FormData();
      formdata.append("image", $file);
      // 按照格式传入对于的图片文件
      uploadImg(formdata).then((res) => {
        console.log(res)
        // res.message为照片链接
        // 请求后端接口后,将pos位置的图片路径替换为后端返回的图片链接
        this.$refs.md.$img2Url(pos, res.message);
      });
    },
    change(value, render) {
      this.content = value
      this.html = render;
    },
  }
};

前端具体代码可参考:https://gitee.com/chenxinyu_cxy/project-upload-img-frontend.git

后端

后端对应的实体类为Article,对应的图片上传接口实现为:

 @PostMapping("/upload")
 public FileResult upload(@RequestParam("image") MultipartFile image) throws FileNotFoundException {
     String path = ResourceUtils.getURL("classpath:").getPath()+"static/image";
     File filePath = new File(path);
     if (!filePath.exists() && !filePath.isDirectory()) {
         filePath.mkdirs();
     }

     //获取原始文件名称(包含格式)
     String originalFileName = image.getOriginalFilename();
     assert originalFileName != null;
     //获取文件后缀名
     String type = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
     //获取文件名(没有后缀)
     String name = originalFileName.substring(0, originalFileName.lastIndexOf("."));
     String fileName = UUIDUtils.getUuid() + name + "." + type;

     //在指定路径下创建一个文件
     File targetFile = new File(path, fileName);

     //将文件保存到服务器指定位置
     try {
         image.transferTo(targetFile);
         //将文件在服务器的存储路径返回(此处为本地路径)
         return new FileResult(true,"http://127.0.0.1:8080/image/"+fileName,path+"/"+fileName);
     } catch (IOException e) {
         return new FileResult(false, "上传失败","");
     }
 }

后端具体代码可参考:https://gitee.com/chenxinyu_cxy/project-file-upload-backend.git

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3.2.13和Mavon Editor是两个不同的库,它们可以在Vue项目中一起使用来实现富文本编辑器的功能。 首先,你需要在你的Vue项目中安装Vue 3.2.13和Mavon Editor。你可以使用npm或yarn来安装它们。在命令行中运行以下命令: ```shell npm install vue@3.2.13 npm install mavon-editor ``` 安装完成后,你需要在你的Vue项目中引入VueMavon Editor。在你的Vue组件中,可以按照以下方式引入它们: ```javascript import { createApp } from 'vue'; import App from './App.vue'; import MavonEditor from 'mavon-editor'; import 'mavon-editor/dist/css/index.css'; const app = createApp(App); app.use(MavonEditor); app.mount('#app'); ``` 在上面的代码中,我们首先引入了VueMavon Editor。然后,我们使用`app.use(MavonEditor)`来注册Mavon Editor插件。最后,我们使用`app.mount('#app')`将Vue应用挂载到HTML页面上的一个元素上。 现在,你可以在你的Vue组件中使用Mavon Editor了。在模板中,你可以使用`<mavon-editor></mavon-editor>`标签来渲染Mavon Editor组件。例如: ```html <template> <div> <mavon-editor v-model="content"></mavon-editor> </div> </template> <script> export default { data() { return { content: '' }; } }; </script> ``` 在上面的代码中,我们使用`v-model`指令将Mavon Editor的内容绑定到了`content`属性上。这样,当用户在编辑器中输入内容时,`content`属性的值也会相应地更新。 这就是使用Vue 3.2.13和Mavon Editor实现富文本编辑器的基本步骤。你可以根据自己的需求进一步定制和配置Mavon Editor的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值