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,即可看到文件上传界面。
相关推荐
武子康10 分钟前
大数据-253 离线数仓 - Airflow 入门与任务调度实战:DAG、Operator、Executor 部署排错指南
大数据·后端·apache hive
IT_陈寒33 分钟前
深入理解JavaScript:核心原理与最佳实践
前端·人工智能·后端
树獭叔叔39 分钟前
GRPO:比PPO更简单的RLHF算法
后端·aigc·openai
shelter41 分钟前
并发操作session对象导致登录闪退问题
后端
兆子龙1 小时前
TypeScript高级类型编程:从入门到精通
前端·后端
IT_陈寒1 小时前
Python开发者的效率革命:这5个技巧让你的代码提速50%!
前端·人工智能·后端
MekoLi291 小时前
Spring AI 与 LangChain4j 从入门到精通:Java 后端开发者的 AI 实战手册
后端·面试
树獭叔叔1 小时前
从RLHF到PPO:让AI学会说人话
后端·aigc·openai
Meepo_haha1 小时前
创建Spring Initializr项目
java·后端·spring
Memory_荒年1 小时前
SpringBoot事务源码深度游:从注解到数据库的“奇幻漂流”
java·后端·spring