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

本文介绍了如何在Vue2和SpringBoot项目中集成mavon-editor,实现在编辑器中上传图片的功能。首先通过npm安装mavon-editor并在main.js中引入,然后在Vue组件中使用mavon-editor,监听@imgAdd事件处理图片上传,调用后端接口uploadImg。后端接收到图片后,保存到静态资源目录并返回图片链接。文章提供了前端和后端的完整代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值