springboot +vue+elupload图片回显

时间: 2023-10-02 20:10:13 浏览: 42
在 Spring Boot 中使用 Vue.js 来上传图片并回显可以通过以下步骤来实现: 1. 在 Vue.js 中使用 el-upload 组件进行图片上传。 ``` <template> <el-upload class="upload-demo" action="/api/upload" :on-success="handleSuccess" :before-upload="beforeUpload"> <el-button size="small" type="primary">点击上传</el-button> </el-upload> <img :src="imageUrl" v-if="imageUrl"> </template> <script> export default { data() { return { imageUrl: '' } }, methods: { beforeUpload(file) { const isJPG = file.type === 'image/jpeg' const isPNG = file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG && !isPNG) { this.$message.error('只能上传 JPG/PNG 格式的图片') return false } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB') return false } return true }, handleSuccess(res) { this.imageUrl = res.data.url } } } </script> ``` 2. 在 Spring Boot 中编写上传接口。 ``` @RestController @RequestMapping("/api") public class UploadController { @Value("${upload.path}") private String uploadPath; @PostMapping("/upload") public Result upload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return Result.fail("上传失败,请选择文件"); } String fileName = file.getOriginalFilename(); String filePath = uploadPath + fileName; File dest = new File(filePath); try { file.transferTo(dest); return Result.success("上传成功", new UploadResult(fileName, filePath)); } catch (IOException e) { e.printStackTrace(); } return Result.fail("上传失败"); } @Slf4j @Data @AllArgsConstructor class UploadResult { private String fileName; private String filePath; } } ``` 3. 在 Vue.js 中获取上传成功后的图片地址,并显示在页面中。 ``` handleSuccess(res) { this.imageUrl = res.data.url } ``` 4. 在 Spring Boot 中提供获取图片接口。 ``` @GetMapping("/image/{fileName}") public void getImage(@PathVariable("fileName") String fileName, HttpServletResponse response) { String filePath = uploadPath + fileName; File file = new File(filePath); if (file.exists()) { response.setContentType("image/jpeg"); try (FileInputStream fileInputStream = new FileInputStream(file)) { IOUtils.copy(fileInputStream, response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } } ``` 5. 在 Vue.js 中通过图片地址来获取图片。 ``` <img :src="imageUrl" v-if="imageUrl"> ``` 6. 最后需要在 Spring Boot 中配置上传文件路径。 ``` spring: servlet: multipart: max-file-size: 10MB max-request-size: 10MB enabled: true resources: static-locations: file:/path/to/upload/folder/ ``` 以上是上传图片并回显的实现过程,你可以根据自己的需求来进行修改和扩展。

相关推荐

最新推荐

recommend-type

tomcat部署springboot+vue.doc

政府门户网站,企业官网等基本都是web项目,web项目所使用的技术也是在不断的更新,前几年的技术基本上都是jsp+ssh,到后来的h5+ssh,h5+spring+mybatis,目前使用最多的是springboot+VUE前后端分离的技术,...
recommend-type

springboot+vue实现websocket配置过程解析

主要介绍了springboot+vue实现websocket配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

SpringBoot + Vue 项目部署上线到Linux 服务器的教程详解

给大家分享以下我是如何部署 SpringBoot + Vue 前后端分离的项目的,我用的 Linux 发行版是 CentOS7.5 有了一个基于 ElementUI 的电商后台管理系统,在开发一个相似的后台就会轻松很多。不过前面的系统的后端是使用 ...
recommend-type

Springboot+Vue+shiro实现前后端分离、权限控制的示例代码

主要介绍了Springboot+Vue+shiro实现前后端分离、权限控制的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

SpringBoot+Vue+Redis实现单点登录(一处登录另一处退出登录)

小编接到一个需求,需要实现用户在浏览器登录后,跳转到其他页面,当用户在其它地方又登录时,前面用户登录的页面退出登录,这篇文章主要介绍了SpringBoot+Vue+Redis实现单点登录,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。