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

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

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

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

<template>
  <div class="container">
    <el-upload
        class="upload-demo"
        drag
        action="/xml/fileUpload"
        multiple
        accept=".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 < 2
      if(!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();
    }
}

相关推荐

  1. SpringBoot+Vue实现简单文件(策略模式)

    2024-07-14 13:56:02       24 阅读
  2. ByteToMessageDecoder&简单实现文件

    2024-07-14 13:56:02       40 阅读
  3. django关于文件分块简单实现(template+view)

    2024-07-14 13:56:02       40 阅读
  4. (图片)文件功能实现

    2024-07-14 13:56:02       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 13:56:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 13:56:02       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 13:56:02       58 阅读
  4. Python语言-面向对象

    2024-07-14 13:56:02       69 阅读

热门阅读

  1. LeetCode 367, 56, 22

    2024-07-14 13:56:02       20 阅读
  2. 【关于Pinia与Vuex】

    2024-07-14 13:56:02       15 阅读
  3. Swift 基于Codable协议使用

    2024-07-14 13:56:02       20 阅读
  4. 升级springboot3.2集成shiro的问题

    2024-07-14 13:56:02       28 阅读
  5. 后端老鸟的前端初探:心得与领悟20240713

    2024-07-14 13:56:02       24 阅读