vue + UEditor 上传图片(回显),上传附件 含token

@[TOC](vue + UEditor 上传图片(回显),上传附件 含token)

背景介绍

本来一开始用的是TinyMCE的文本编辑器,但是谁还没遇到个无理的甲方爸爸呢,硬要换成已经不再维护好久的ueditor,后台不愿意改接口,只好硬着头皮上了

安装

参考vue-ueditor-wrap的官网安装就行
我用的vue2

npm i vue-ueditor-wrap@2.x
# 或者
yarn add vue-ueditor-wrap@2.x

下载ueditor,我下载的是ueditor-dev-1.5.0,不多说
下载解压后放在static文件夹下
在这里插入图片描述
这是我的页面结构,把富文本编辑器单独变成一个组件,接口以项目实际为准

<template>
  <div>
    <vue-ueditor-wrap
      v-model="content"
      :config="editorConfig"
      editor-id="editor"
    ></vue-ueditor-wrap>
      
<!--@ready="ueditorReady"  -->
  </div>
</template>
<script>
import VueUeditorWrap from "vue-ueditor-wrap";
export default {
   
  data() {
   
    return {
   
      editorConfig: {
   
        // 编辑器不自动被内容撑高
        autoHeightEnabled: true,
        serverUrl: "http://xxx.com/api/uploads", // 服务端地址
        imageActionUrl:"http://xxx.com/api/uploads",//图片上传地址
        fileActionUrl:"http://xxx.com/api/uploads",
        imageAllowFiles:[".png",".jpg",".jpeg",",gif"],//图片上传类型限制
        imageMaxSize:2048000,//图片限制大小2M以内
        fileAllowFiles:[".pdf","doc","docx","zip","rar"],//文件上传类型限制
      },
      content: "", //网页内容
    };
  },
  components: {
   
    VueUeditorWrap,
  },
  watch: {
   
    content(val) {
   
      this.$emit("onEditorChange", val);
    },
  },
};
</script>
<style lang="less" scoped>
</style>

配置ueditor根路径

不清楚的可以看ueditor官网配置在这里插入图片描述
接着把ueditor.all.js重命名为ueditor.all.min.js便于修改
这是我的接口结构,请求时需要头部加token,然后加上参数type区别文件类型,上传文件后,接口返回访问链接
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

修改图片并回显

首先修改ueditor.all.min.js
大概在8830行的位置

// core/loadconfig.js
  (function () {
   
    UE.Editor.prototype.loadServerConfig = function () {
   
      var me = this;
      setTimeout(function () {
   
        try {
   
          me.options.imageUrl &&
            me.setOpt(
              "serverUrl",
              me.options.imageUrl.replace(
                /^(.*[\/]).+([\.].+)$/,
                "$1controller$2"
              )
            );

          var configUrl = me.getActionUrl("serverUrl"),
          isJsonp = utils.isCrossDomainUrl(configUrl);
          console.log(isJsonp, 'isJsonp',configUrl)
          /* 发出ajax请求 */
          configUrl &&
            UE.ajax.request(configUrl, {
   
              method: 'POST',
              dataType: 'json',
              data: {
   },
              onsuccess: function (r
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现Spring Boot + Vue + ElementUI 的图片回显功能,需要以下步骤: 1. 在Vue中使用ElementUI的Upload组件上传图片,并将上传成功后的图片地址保存在Vue的data中。 2. 在Spring Boot的Controller中编写接收图片上传请求的方法,将上传的图片保存到服务器上,并返回图片的访问地址。 3. 在Vue中使用img标签展示上传成功的图片,将img标签的src属性设置为上传成功后返回的图片地址。 下面是一个简单的示例代码: Vue代码: ``` <template> <div> <el-upload class="upload-demo" :action="uploadUrl" :on-success="handleUploadSuccess" :show-file-list="false"> <el-button slot="trigger" size="small" type="primary">点击上传</el-button> </el-upload> <img v-if="imageUrl" :src="imageUrl" /> </div> </template> <script> export default { data() { return { imageUrl: '', uploadUrl: 'http://localhost:8080/upload' } }, methods: { handleUploadSuccess(response) { this.imageUrl = response.data.url; } } } </script> ``` Spring Boot代码: ``` @RestController public class UploadController { @PostMapping("/upload") public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) { try { // 保存上传的文件到服务器上 String fileName = file.getOriginalFilename(); String filePath = "path/to/save/" + fileName; File dest = new File(filePath); file.transferTo(dest); // 返回图片的访问地址 String imageUrl = "http://localhost:8080/images/" + fileName; return ResponseEntity.ok(imageUrl); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } } ``` 注意:上述代码仅供参考,具体实现需要根据自己的需求进行适当的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值