el-upload上传图片以及数量大小限制

<template>
    <div>
        <el-upload multiple :limit="5" :action="imgUploadUrl" list-type="picture-card"
            :headers="headers" :on-remove="handleRemove" :data="importData" 
            :on-success="onSuccess" ref="files" :file-list="huixianArr" 
            :on-preview="handlePictureCardPreview" :before-upload="beforeAvatarUpload" 
            :on-exceed="onExceed" :on-change="handleEditChange" 
            :class="{ hide: hideUploadBtn }">
          <i class="el-icon-plus"></i>
        </el-upload>
        <!-- 预览图片 -->
        <el-dialog :visible.sync="dialogVisible" :title="dialogImageTitle" 
            append-to-body top="62px">
            <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
    </div>
</template>

<script>
    data(){
        return{
            imglist: [],
            huixianArr: [],
            dialogImageUrl: "",
            imgUploadUrl: `${base.prodBaseUrl}/file/upload`,
            headers: {
                Authorization: "Bearer " + sessionStorage.getItem("access_token"),
            },
            importData: {
                type: "AG",
            },
            dialogImageTitle: "",
            dialogVisible: false,
            hideUploadBtn: false,
        }
    },
    methods:{
         // 图片上传成功回调
         onSuccess(res) {
            if (res.code === 200) {
                this.imglist.push(res.data[0].path);
            }
        },
        // 删除图片
        handleRemove(file, fileList) {
            let temp = [];
            fileList.forEach((element) => {
                temp.push(element.url);
            });
            this.imglist = temp;
            this.hideUploadBtn = temp.length >= 5;
        },
        // 最多上传6张图,超过时隐藏上传按钮
        handleEditChange(file, fileList) {
            this.hideUploadBtn = fileList.length >= 5;
        },
        // 预览图片
        handlePictureCardPreview(file) {
            this.dialogImageTitle = file.name;
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        onExceed(file, fileList) {
            this.$message.warning("最多上传5张图片!");
        },
        beforeAvatarUpload(file) {
            // const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            // if (!isJPG) {
            //     this.$message.error('上传头像图片只能是 JPG 格式!');
            // }
            if (!isLt2M) {
                this.$message.warning("上传图片大小不能超过 2MB!");
            }
            return isLt2M;
        },
    }
</script>

<style lang="scss" scoped>
    // 图片数量等于限制数量时隐藏上传组件
    ::v-deep.hide {
        .el-upload--picture-card {
            display: none !important;
        }
    }
</style>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值