Vue中删除重复上传的文件


上传控件:

<el-upload class="upload-demo"  :on-change="filesChange">


filesChange方法:

filesChange(file, fileList) {
                if (file.size > 2000000) {
                    this.$message({
                        showClose: true,
                        message: "单个文件大小必须小于2Mb",
                        type: "warning"
                    });
                    var index = fileList.indexOf(file);
                    if (index > -1) {
                        fileList.splice(index, 1);
                    }
                }

                //判断文件名是否重复
                var count = 0
                var index = 0    // 记录要删除的文件下标               
                fileList.forEach((item, idx) => {
                    //在此处,对比文件名,将文件名相同的对比次数累加,
                    // 相同的文件名累加值为 2 时,说明文件名已经重复,直接删掉。
                    if (file.name == item.name) {
                        count++
                        if (count == 1) {
                            index = idx;
                        }
                        if (count === 2) {
                            this.$message({
                              message: file.name + '文件已存在',
                              type: 'info'
                            })              
                            fileList.splice(index, 1)
                        }
                    }
                })                
            },
原文地址:https://www.cnblogs.com/ahao214/p/11057750.html