springBoot与ElementUI配合上传文件

以下是使用Vue CLI创建的Vue项目,结合Element UI来实现文件上传功能的完整示例。

步骤

  1. 创建Vue项目:确保你已经安装了Vue CLI,若未安装,可使用以下命令安装:
bash 复制代码
npm install -g @vue/cli

然后创建一个新的Vue项目:

bash 复制代码
vue create element-file-upload-demo
cd element-file-upload-demo
  1. 安装Element UI:在项目根目录下执行以下命令安装Element UI:
bash 复制代码
npm install element-ui -S
  1. 配置Element UI :在src/main.js中引入Element UI:
javascript 复制代码
import Vue from 'vue';
import App from './App.vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

new Vue({
  render: h => h(App),
}).$mount('#app');
  1. 编写后端接口(使用Spring Boot):后端代码和之前的示例相同,这里再给出一遍。
java 复制代码
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        if (file.isEmpty()) {
            result.put("success", false);
            result.put("message", "上传的文件为空");
            return result;
        }
        try {
            String filePath = "upload/" + file.getOriginalFilename();
            File dest = new File(filePath);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            file.transferTo(dest);
            result.put("success", true);
            result.put("message", "文件上传成功");
        } catch (IOException e) {
            result.put("success", false);
            result.put("message", "文件上传失败:" + e.getMessage());
        }
        return result;
    }
}
  1. 编写前端组件(Vue) :在src/components目录下创建FileUpload.vue组件:

  2. App.vue中使用组件

vue 复制代码
<template>
  <div id="app">
    <FileUpload />
  </div>
</template>

<script>
import FileUpload from './components/FileUpload.vue';

export default {
  name: 'App',
  components: {
    FileUpload
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

运行项目

  1. 启动Spring Boot项目。
  2. 在Vue项目根目录下执行npm run serve启动前端项目。
  3. 打开浏览器访问http://localhost:8080,即可看到文件上传界面。
相关推荐
未来影子9 分钟前
SpringAI(GA):Nacos2下的分布式MCP
后端
用户868181248049 分钟前
【ruoyi-vue-pro】用户相关的设计
后端
迢迢星万里灬32 分钟前
Java求职者面试:Spring、Spring Boot、Spring MVC与MyBatis技术深度解析
java·spring boot·spring·面试·mybatis·spring mvc
天天摸鱼的java工程师33 分钟前
Nacos 2.0 + 为啥非要三个端口?一次踩坑实录
java·后端
SimonKing34 分钟前
5分钟了解,Mysql事务隔离级别
java·后端·架构
Moonbit35 分钟前
双周报Vol.73:移除使用方法实现 trait 、新增了 “错误多态” 功能、.语法支持使用 _ 的匿名函数...
后端·算法
代码老y37 分钟前
基于springboot的图书管理系统的设计与实现
java·vue.js·spring boot·后端·毕业设计·课程设计·个人开发
ross38 分钟前
更新已打包好的 Spring Boot JAR 文件中的 class 文件
spring boot·后端·jar
用户6757049885021 小时前
localhost 和 127.0.0.1 到底有啥区别?
后端
写bug写bug1 小时前
彻底搞懂响应式编程
java·后端·spring