当前位置: 首页 > news >正文

SpringBoot+Vue实现简单的文件上传(txt篇)

SpringBoot+Vue实现简单的文件上传

1 环境 SpringBoot 3.2.1,Vue 2,ElementUI
2 页面在这里插入图片描述

3 效果:只能上传txt文件且大小限制为2M,选择文件后自动上传。
4 前端代码

<template><div class="container"><el-uploadclass="upload-demo"dragaction="/xml/fileUpload"multipleaccept=".txt":before-upload="beforeUpload"><i class="el-icon-upload"></i><div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div><div class="el-upload__tip"><slot name="tip" > 只能上传 txt 文件,且不超过2M</slot></div></el-upload></div>
</template><script>
// import axios from "axios";export default {name: 'App',data() {const data = [];return {}},watch: {},created() {},methods: {beforeUpload(file){console.log(file.type)const isText = file.type == "text/plain"const isLt2M = file.size /1024 /1024 < 2if(!isText){this.$message.error("只能上传txt文件!")}if(!isLt2M){this.$message.error("文件大小超过限制!")}}}
}
</script><style>
.container {display: flex;
}
</style>

5 后端代码

package org.wjg.onlinexml.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.wjg.onlinexml.po.Result;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;@RestController
public class FileController {@RequestMapping("/fileUpload")private Result getXml(@RequestParam("file") MultipartFile file) {if (file.isEmpty()) {return Result.builder().code(500).msg("上传失败!").build();}try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {String line;while ((line = reader.readLine()) != null) {// 在这里处理读取到的每一行内容System.out.println(line);}} catch (IOException e) {e.printStackTrace();}return Result.builder().code(200).msg("上传成功").build();}
}
http://www.lryc.cn/news/400257.html

相关文章:

  • LLMs之RAG:GraphRAG(本质是名词Knowledge Graph/Microsoft微软发布)的简介、安装和使用方法、案例应用之详细攻略
  • Linux 之前的 Unix 桌面沉浮启示录
  • 面试问题梳理:项目中防止配置中的密码泄露-Jasypt
  • engine.addImportPath()用于向 QML 引擎添加新的模块搜索路径
  • ServiceNow UI Jelly模板注入漏洞复现(CVE-2024-4879)
  • 项目部署笔记
  • PyCharm\VsCode——Python第三方库下载换源
  • 图片上传裁剪react-cropper
  • 跨越空间的编码:在PyCharm中高效使用远程解释器
  • Vue3单文件jsx输出多组件示例遇到的坑
  • OpenCV中的轮廓检测cv2.findContours()
  • JFlash读取和烧录加密stm32程序
  • 【总结】实际业务场景中锁、事务、异常如何考虑使用?
  • Pytorch使用Dataset加载数据
  • 【nginx】nginx的优点
  • K8S ingress 初体验 - ingress-ngnix 的安装与使用
  • qt 获取父控件
  • flask基础配置详情
  • 单相整流-TI视频课笔记
  • 用GPT 4o提高效率
  • 20240711每日消息队列-------------MQ消息的积压的折磨
  • 推荐一个比 Jenkins 使用更简单的项目构建和部署工具
  • java 在pdf中根据关键字位置插入图片(公章、签名等)
  • 施耐德EOCR系列电机保护器全面升级后無端子型
  • 27.数码管的驱动,使用74HC595移位寄存器芯片
  • TCP/IP 原理、实现方式与优缺点
  • 利率债与信用债的区别及其与债券型基金的关系
  • linux下解压命令
  • Vulnhub靶场DC-3-2练习
  • Swift入门笔记