springboot+ thymeleaf 实现文件上传

Controller层

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;

@Controller
public class FileUploadController {

    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("files") MultipartFile[] files, Model model) {
        boolean allSuccessful = true; // 假设所有文件上传都成功,根据实际情况修改此逻辑
        for (MultipartFile file : files) {
            // 你的文件上传逻辑...
            if (!uploadSuccessful) {
                allSuccessful = false;
                break;
            }
        }

        if (allSuccessful) {
            model.addAttribute("successMessage", "所有文件上传成功!");
        } else {
            model.addAttribute("errorMessage", "文件上传过程中发生错误!");
        }
        return "uploadResult"; // 返回到展示结果的Thymeleaf页面
    }
}

前端thymeleaf

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>文件上传结果</title>
</head>
<body>
    <!-- 成功提示 -->
    <div th:if="${successMessage}" class="alert alert-success" role="alert">
        <span th:text="${successMessage}"></span>
    </div>

    <!-- 失败提示 -->
    <div th:if="${errorMessage}" class="alert alert-danger" role="alert">
        <span th:text="${errorMessage}"></span>
    </div>
    
    
</body>
</html>

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值