element ui el-upload上传附件,删除,预览,上传前验证,删除前验证

表单项:
file-list=“fileList” 上传附件显示文件名称
limit=“1” 限制上传数量为1

<el-form-item label="附件">
                        <el-upload
                                class="upload-demo"
                                :action="uploadUrl()"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload"
                                :on-remove="handleRemove"
                                :before-remove="beforeRemove"
                                :on-preview="previewAtt2"
                                :file-list="fileList"
                                :limit="1">
                            <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>
                    </el-form-item>

data 定义:

			 //附件内容
            uploadData:[],
            //附件内容(初始化用)
            fileList:[],

方法:

// 上传
            uploadUrl() {
                return ctx+"file/uploadFile";
            },
            //上传成功
            handleAvatarSuccess(res, file) {
                const files = {
                    uid: file.uid,
                    fileName: res.data.fileName,
                    fileType: res.data.fileType,
                    fileSize: res.data.fileSize,
                    filePath: res.data.filePath,
                    bizType: "合同扫描件",
                    url: file.url
                };
                this.uploadData.push(files);
            },
            //上传之前
            beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 10;
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 10MB!');
                }
                return isLt2M;
            },
            //删除
            handleRemove(file) {
                for(var i = 0; i < this.uploadData.length; i++){
                    if(file.uid == this.uploadData[i].uid){
                        this.uploadData.splice(i,1)
                    }
                }
            },
           // 删除前验证
            beforeRemove(file){
               
            },
            //点击名称预览文件
            previewAtt2(file) {
                if(file.url){
                    window.open(ctx+"upload/"+file.url, '_blank');
                }else{
                    var url = file.filePath;
                    window.open(ctx+"upload/"+url, '_blank');
                }
            },
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值