若依修改,如何安装wangEditor,图片上传接口编写。建议暴露专门写一个图片存储的接口

简介: 若依修改,如何安装wangEditor,图片上传接口编写。建议暴露专门写一个图片存储的接口

安装 | wangEditor

参考资料:

若依修改,配置了一个接口路径出现了,如何放通接口{ “msg“: “请求访问:/code/list,认证失败,无法访问系统资源“, “code“: 401}

若依修改,配置了一个接口路径出现了,如何放通接口{ “msg“: “请求访问:/code/list,认证失败,无法访问系统资源“, “code“: 401}-CSDN博客

文本-----富文本图片上传手工资料(上)实现图片上传和下载简单代码

文本-----富文本图片上传手工资料(上)实现图片上传和下载简单代码_富文本 图片自动下载-CSDN博客

第一步npm

npm install @wangeditor/editor --save

第二步,创建一个页面write.vue

粘贴代码:

<template>
    <div class="container" style="margin: -8px 0 0 0">
    <el-form
        :model="ruleForm"
        status-icon
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm" >
  <el-form-item  form-item prop="content">
      <div style="border: 1px solid #ccc">
      <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" :mode="mode" />
      <Editor style="height: 500px; overflow-y: hidden;" v-model="ruleForm.code" :defaultConfig="editorConfig" :mode="mode"
      @onCreated="handleCreated" />
      </div>
  </el-form-item>
 
  <div class="main">
    <el-form-item label="标题" prop="title">
        <el-input
         type="text"
         v-model="ruleForm.title"
         ></el-input>
    </el-form-item>
    <el-form-item label="介绍" prop="introduce">
        <el-input
          type="text"
          v-model="ruleForm.introduce"
          ></el-input>
    </el-form-item>
    <el-form-item label="分类" prop="category">
        <el-input
          type="text"
          v-model="ruleForm.category"
          ></el-input>
    </el-form-item>
  </div>
  </el-form>
  <!-- <div style="border: 1px solid #ccc">
  <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" :mode="mode" />
  <Editor style="height: 500px; overflow-y: hidden;" v-model="valueHtml" :defaultConfig="editorConfig" :mode="mode"
    @onCreated="handleCreated" />
  </div> -->  
  <div class="mb-4 foot" style="margin: 40px 0 0 940px;">
      <el-button round >保存</el-button>
      <el-button type="primary" round @click="submitForm()">提交</el-button>
  </div>
    </div>
    </template>
  <script setup>
  import '@wangeditor/editor/dist/css/style.css' // 引入 css
  import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
  import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  const editorRef = shallowRef()
  
  // 内容 HTML
  const valueHtml = ref('<p>hello</p>')
  
  // 模拟 ajax 异步获取内容
  onMounted(() => {
   setTimeout(() => {
     valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>'
   }, 1500)
  })
  
  const toolbarConfig = {}
  const editorConfig = { placeholder: '请输入内容...',
  MENU_CONF: {}
  }
  
  // 组件销毁时,也及时销毁编辑器
  onBeforeUnmount(() => {
   const editor = editorRef.value
   if (editor == null) return
   editor.destroy()
  })
  
  const handleCreated = (editor) => {
   editorRef.value = editor // 记录 editor 实例,重要!
  }
  editorConfig.MENU_CONF["uploadImage"] = {
      fieldName: "image",
      server: "http://localhost:9090/fullText/file/upload",
      base64Limitsize: 5 * 1024
    }
  </script>
  <script>
  import axios from "axios";
  export default {
  data() {
  return {
    ruleForm: {},
    url: "http://localhost:9090/code",
    users: []
  };
  },
  methods: {
  submitForm(){
      // console.log(this.ruleForm)
      let that = this
      if(!this.ruleForm.id){
        
      axios.post(this.url + "/add",this.ruleForm)
      .then(function (res) {
        if (res.data.success) {
          that.$message({
            type: "success",
            message: res.data.data,
          });
          // that.getList(that.pageNum);
          // // 跳转成功,弹框失效
          // that.ruleForm = {}
          // that.dialogVisible = false
        } else {
          that.$message({
            type: "error",
            message: res.data.message,
          });
        }
      })
      .catch(function (error) {
        alert("error!");
        console.log(error);
      });
      } else {
          axios.post(this.url+'/modify',this.ruleForm).then(function (res) {
                      if(res.data.success){
                          that.$message({
                              type: 'success',
                              message: res.data.data
                          })                              
                      }else{
                          that.$message({
                              type: 'error',
                              message: res.data.message
                          })
                      }
                  }).catch(function (error) {
                      alert('error!')
                      console.log(error)
                  })
      }
  },
  handleTableFileUpload(row,file,fileList){
        console.log(row,file,fileList)
        row.avatar = file.response.data;
        // this.$set(row,'avatar',file.response.data)
        console.log(row)
        axios.put('/user/update',row).then(res => {
          if (res.data.code === '200'){
            this.$message.success('上传成功')
          } else {
            this.$message.error(res.data.msg)
          }
        })
      },
      preview(url){
        window.open(url)
      },
      handleMultipleFileUpload(response,file,fileList){
        console.log(response,file,fileList)
        this.urls = fileList.map(v => v.response?.data)
      },
      handleFileUpload(response,file,fileList){
        this.fileList = fileList
      }
  }
  }
  </script>
  <style scoped>
    html{
      margin: 0;
      padding: 0;
    }
    .container{
      background-color: #f5f6f7 !important;
      height: 100vh; 
    }
    .main {
      margin: 0 200px 0 100px;
      padding: 20px 0;
      background-color: #fff;
    }
  </style>

第三步 配置静态路由

  {
    path: '/write',
    component: () => import('@/views/project/article/write')
  },

第四步,permission中放开静态权限

第五步,wangEditor配置完毕之后,报vue.runtime.esm.js:620 [Vue warn]: Invalid handler for event "onCreated": got undefined

found in,后端接口没配

第六步配后端接口,配置图片接口

第七步:后端编写上传图片接口,后端插入依赖

 
<dependency>
     <groupId>commons-io</groupId>
     <artifactId>commons-io</artifactId>
     <version>2.11.0</version>
</dependency>

第八步:创建FullTextController文件,放入这一串代码:

package com.ruoyi.web.controller.part;
 
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.UUID;
 
@RequestMapping("/fullText")
@RestController
public class FullTextController {
    private static final String STORE_DIR = "D:\\project\\零一电科\\技术资料\\lingyidianke\\zeroBackEnd\\ZerosBackEnd\\src\\main\\resources\\pict\\";
    private static final String DOMAIN = "http://localhost:9090/fullText/file/download/";
    static class Success {
        public final int errno;
        public final Object data;
        public Success(String url) {
            this.errno = 0;
            HashMap<String, String> map = new HashMap<>();
            map.put("url", url);
            this.data = map;
        }
    }
    @RequestMapping("/file/upload")
    public Object uploadPict(@RequestParam("image") MultipartFile file) throws IOException {
        // 获取文件流
        // 获取文件真实名字
        String fileName = UUID.randomUUID().toString().substring(0, 10) + file.getOriginalFilename();
        // 在服务器中存储文件
        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(STORE_DIR + fileName));
        // 返回图片url
        String url = DOMAIN + fileName;
        return new Success(url);
    }
    @GetMapping("/file/download/{fileName}")
    public void download(@PathVariable("fileName") String fileName, HttpServletRequest request, HttpServletResponse response) {
        // 获取真实的文件路径
        String filePath = STORE_DIR + fileName;
        System.out.println("++++完整路径为:"+filePath);
        try {
            // 下载文件
            // 设置响应头
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
            // 读取文件内容并写入输出流
            Files.copy(Paths.get(filePath), response.getOutputStream());
            response.getOutputStream().flush();
        } catch (IOException e) {
            response.setStatus(404);
        }
    }
}

第九步:想改配置修改需要注意事项

Java路径的相关写法,如何定义在某一具体的文件,本地跑的项目用绝对路径,没有问题

比如,我想写在D盘的路径下,可以这样写

private static final String STORE_DIR = "D:\\project\\零一电科\\技术资料\\lingyidianke\\zeroBackEnd\\ZerosBackEnd\\src\\main\\resources\\pict\\";

但是后期变成部署,想要写成相对路径,点击Copy Path

复制这里的内容,就是相对路径

自己src的路径写法写成了,建议暴露专门写一个图片存储的接口

"\\src\\main\\resources\\pict\\";

最后src的存入的图片就是这样了

能用postman测试就行,若依的话,还要放开权限


相关文章
|
2月前
|
JavaScript 前端开发 算法
简化文件上传流程:学习如何封装Vue2拖拽上传组件
简化文件上传流程:学习如何封装Vue2拖拽上传组件
72 0
|
4天前
|
Java
软件开发常用之SpringBoot文件下载接口编写(下),Vue+SpringBoot文件上传下载预览,服务器默认上传是1M,可以调节,调节文件上传大小写法,图片预览,如何预览后下次还能看到,预览写法
软件开发常用之SpringBoot文件下载接口编写(下),Vue+SpringBoot文件上传下载预览,服务器默认上传是1M,可以调节,调节文件上传大小写法,图片预览,如何预览后下次还能看到,预览写法
|
3天前
|
JSON JavaScript 前端开发
若依修改,若依如何发送get和post请求,发送数据请求的写法,若依请求的API在src的api文件下,建立请求的第一步,在API中新建一个文件,第二步新建JavaScript文件
若依修改,若依如何发送get和post请求,发送数据请求的写法,若依请求的API在src的api文件下,建立请求的第一步,在API中新建一个文件,第二步新建JavaScript文件
若依修改,修改代理线上接口登录后台,若依框架如何使用线上的接口,如何在本地获取网页上的接口
若依修改,修改代理线上接口登录后台,若依框架如何使用线上的接口,如何在本地获取网页上的接口
|
1月前
|
Java 机器人 API
JAVA实现自动打开URL对应的网页并保存为图片-不借助第三方API
JAVA实现自动打开URL对应的网页并保存为图片-不借助第三方API
26 9
|
5天前
|
前端开发 JavaScript Java
文本----简单编写文章的方法(中),后端接口的编写,自己编写好页面就上传到自己的服务器上,使用富文本编辑器进行编辑,想写好一个项目,先分析一下需求,再理一下实现思路,再搞几层,配好参数校验,lomb
文本----简单编写文章的方法(中),后端接口的编写,自己编写好页面就上传到自己的服务器上,使用富文本编辑器进行编辑,想写好一个项目,先分析一下需求,再理一下实现思路,再搞几层,配好参数校验,lomb
|
2月前
|
JavaScript 前端开发
若依 自定义实现导入功能
若依 自定义实现导入功能
57 1
|
编解码
调用后台接口实现Excel导出功能以及导出乱码问题解决
调用后台接口实现Excel导出功能以及导出乱码问题解决
205 0
调用后台接口实现Excel导出功能以及导出乱码问题解决
|
2月前
|
前端开发
elementui-upload组件自定义样式上传(upload中常用的属性,但是网络上却找不到教程)(解决bug删除之后再次上传会上传删除的图片)专注后端工程师的前端速成
elementui-upload组件自定义样式上传(upload中常用的属性,但是网络上却找不到教程)(解决bug删除之后再次上传会上传删除的图片)专注后端工程师的前端速成
165 0
|
12月前
|
编解码
vue3+element表格调用后台接口实现Excel导出功能以及导出乱码问题解决
vue3+element表格调用后台接口实现Excel导出功能以及导出乱码问题解决
579 0