vue实现文件上传,前后端

前端封装el-upload组件,父组件传值dialogVisible(用于显示el-dialog),子组件接收,并且关闭的时候返回一个值(用于隐藏el-dialog),最多上传五个文件,文件格式为.jpg\pdf\png

复制代码
<template>
    <div>
       <el-dialog  width="30%"  :visible.sync="dialogShow" append-to-body @close='handleCancle' title="上传发票" class="uploadDialog">
        <!-- list-type="picture" -->
        <el-upload
           ref="upload"
           :auto-upload="false"
           :http-request="uploadFile"
           :on-change="changeFileLength"
           :limit="5"
           :on-exceed="handleExceed"
           action=""
           accept=".pdf,.jpg,.png" 
           multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">点击上传文件</div>
        </el-upload>
        <!-- 上传时点击的按钮 -->
        <el-button @click="upload" type="success">上传文件</el-button>
      </el-dialog>
    </div>
</template>
<script>
import {  upload } from "@/api/invoice/invoiceManagement";

export default {
    name: "uploadCT",
    props:{
        dialogVisible:{
            type:Boolean,
            default:false,
            require:true,
        }
    },
    watch: {
        dialogVisible: {
            handler(val) {
                this.dialogShow = val
            },
            deep: true, // 深度监听
            immediate: true, // 初次监听即执行  
        },
    },

    data(){
        return{
            // 上传文件的列表
            uploadFiles: [],
            // 上传文件的个数
            filesLength: 0,
            // 上传需要附带的信息
            info:{
                id:"",
                name:"",
            },
            //显示
            dialogShow:this.dialogVisible,
        }
    },

    methods:{
        //超出限制提示
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 5 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
        },
        //关闭
        handleCancle(){
            this.uploadFiles= [];
            // 上传文件的个数
            this.filesLength= 0;
            this.dialogShow = false;
            this.$emit('closeUploadDialog',this.dialogShow);
            this.$refs.upload.clearFiles();
        },
        // 修改当前文件列表长度
        changeFileLength(file, fileList){
            this.filesLength = fileList.length
        },

        // 用户点击上传调用
        async upload(){
            // 触发上传 调用配置 :http-request="uploadFile"
            // 即触发 uploadFile函数
            await this.$refs.upload.submit();
            // 上传完成后执行的操作 ...
            this.$modal.msgSuccess("上传成功");
        },

        // 该函数还是会被调用多次
        // 每次param参数传入一个文件
        uploadFile(param){
            console.log("参数",param);
            // 将文件加入需要上传的文件列表
            this.uploadFiles.push(param.file)
            // 当uploadFiles长度等于用户需要上传的文件数时进行上传
            if (this.uploadFiles.length == this.filesLength){
                // 创建FormData上传
                let fd = new FormData()
                // 将全部文件添加至FormData中
                this.uploadFiles.forEach(file => {
                    fd.append('file', file)
                })
                // 将附加信息添加至FormData
                fd.append("id", this.info.id)
                fd.append("name", this.info.name)
                // 配置请求头
                const config = {
                    headers: {
                        "Content-Type": "multipart/form-data",
                    }
                }
                console.log("参数",fd);
                // 上传文件
                upload(fd).then(res => {
                    /*上传成功处理*/
                    console.log(res);
                    if(res.msg=='上传成功'){
                        this.uploadFiles=[];
                        this.filesLength = 0;
                        this.dialogShow = false;
                        this.$emit('closeUploadDialog',this.dialogShow);
                        this.$refs.upload.clearFiles();

                    }
                }).catch(err => {/*报错处理*/});

            }
        }
    }
}
</script>

后端接收

复制代码
 @PostMapping("/upload")
    public AjaxResult upload(@RequestParam(value = "file") MultipartFile[] file)
    {
        try {
            String localPath = "";
            //1.1获取当前日期,当做本地磁盘的目录
            Date nowDate = DateUtils.getNowDate();
            String format = new SimpleDateFormat("YYYYMMDD").format(nowDate);
            String localPathPrefix = "C:\\"+format;
            for(MultipartFile f:file){
                // 获取文件名
                String fileName = f.getOriginalFilename();
                // 获取文件后缀
                String prefix = fileName.substring(fileName.lastIndexOf("."));
                // 保存文件到本地磁盘
                localPath = localPathPrefix+"\\"+fileName;
                File localFile = new File(localPath);
                if (!localFile.getParentFile().exists()) {
                    localFile.getParentFile().mkdirs();
                }
                //写入到本地磁盘
                f.transferTo(localFile);
                // 获取文件在本地磁盘上的路径
                String filePath = localFile.getAbsolutePath();
                log.info("文件名称:"+fileName+"已经存入本地磁盘,全路径为:"+filePath);

                //上传到文件服务器,自己掉接口

                //上传完成后,删除本地临时磁盘文件
                if (localFile.delete()) {
                    log.info(localFile.getName() + "已经删除");
                } else {
                    log.info("文件删除失败");
                }
            }
            //删除本次磁盘的日期目录
            File file1 = new File(localPathPrefix);
            if (file1.delete()) {
                log.info(file1.getName() + "已经删除");
            } else {
                log.info("文件删除失败");
            }

        }catch (Exception e){
            System.out.println(e);
            return error("上传失败");
        }
        return success("上传成功");
    }

效果展示

相关推荐
陈随易2 小时前
Postman 被曝自动往 Claude Code 和 Codex 装插件
前端·后端·程序员
Dream-Y.ocean4 小时前
鸿蒙PC平台宅匠 3D 室内设计适配实战:基于 Electron 壳方案的 Three.js 双渲染引擎与参数化户型编辑器开发
javascript·3d·harmonyos
Csvn5 小时前
性能监控
前端
Highcharts.js5 小时前
Highcharts Gantt使用符号的自定义数据标签开发甘特图示例
javascript·甘特图·highcharts·图表开发·实战代码·响应式图表
Csvn6 小时前
构建工具
前端
FRing036 小时前
vue2干货总结3(computed计算属性、watch监听器)
开发语言·前端·vue.js·vue2
Java小卷7 小时前
JuanForm vs FormCreate!Vue3 表单引擎联动配置 PK!
vue.js·低代码
kyriewen9 小时前
Skills 榜前 10 我全装了:只有 3 个能直接用
前端·javascript·程序员
IT_陈寒9 小时前
当Java的hashCode遇上Lombok,我的缓存被全清了
前端·人工智能·后端
工控丫丫9 小时前
帆软看板中经常使用的公式函数及经常搭的组合
前端·javascript·数据库