el-upload 上传一张图片,并支持预览和删除

该代码段展示了如何在Vue应用中利用ElementPlus组件库实现一个限制为一张图片的上传功能,支持图片预览、删除和大小限制。图片上传前会检查文件大小不超过20MB,超过则显示错误信息。上传成功后,图片URL会被添加到列表中。
<template>

    <el-upload
        class="upload-demo"
        :class="{disabled:cardUpFileList.length>0 ? true:false}"
        :limit="1"
        list-type="picture-card"
        accept=".png,.jpg"
        :file-list="cardUpFileList"
        :before-upload="beforeUploadPath "
        :on-remove="handleRemovePicture"
        :on-preview="handlePicturePreview"
    >
        <el-icon><Plus /></el-icon>
    </el-upload>
    <el-dialog v-model="dialogVisible">
         <img w-full :src="dialogImageUrl" alt="Preview Image" />
    </el-dialog>
</template>

<script setup>
    import {ref,reactive} from 'vue'
    import {ElMessage,ElMessageBox} from 'element-plus'
    import {uploadImg} from '@/api/xx/common.js';  //uploadImg  这个是后台上传文件接口

    let cardUpFileList=ref([]);
    let dialogVisible=ref(false);
    let dialogImageUrl=ref();

    
     //上传图片
    const beforeUploadPath = (file) => {
        const isLt2M = file.size / 1024 / 1024 < 20;
        if (!isLt2M) {
            ElMessage.error('上传图片大小不能超过 20MB!');
            return;
        }
        let fd = new FormData();
        fd.append('file', file);
        uploadImg(fd,'sellerApply').then(res=>{    //uploadImg  这个是后台上传文件接口
            if(res.state!=200){
                ElMessage.error(res.msg);
                return;
            }
            cardUpFileList.value.push({url:res.data.url})
        });
        return false;
    };
    //删除身份证正面图片
    const handleRemovePicture =(file,files)=>{
        cardUpFileList.value = files;
    };
    //图片预览
    const handlePicturePreview= (file,files) => {
        dialogImageUrl.value = file.url;
        dialogVisible.value = true
    }

</script >

<style lang="scss">

    .disabled .el-upload--picture-card {
        display: none;
    }
</style>

 

 

### 解决 `el-upload` 上传图片时无预览删除功能 为了使 `el-upload` 组件支持图片上传预览删除功能,需设置特定属性编写相应逻辑处理函数。具体来说: 对于实现预览功能,在组件内启用 `list-type="picture-card"` 属性可让每次成功上传后的文件显示为卡片形式,附带缩略图效果[^1]。 针对删除操作,则应监听 `on-remove` 事件来响应用户的移除行为;当只允许单张图片存在的情况下,除了调用实例方法 `clearFiles()` 清空队列外,还需手动更新用于存储已选图像路径的数据字段(如 `urlList` 数组),防止旧有记录残留影响新选择的结果呈现[^2]。 下面给出一段完整的 Vue 单文件组件 (SFC) 示例代码片段,展示了如何配置上述特性: ```vue <template> <div class="upload-demo"> <!-- 使用 picture-card 列表样式 --> <el-upload action="#" list-type="picture-card" :auto-upload="false" :limit="1" :file-list="fileList" @remove="handleRemove" ref="uploadRef" > <i slot="default" class="el-icon-plus"></i> <div v-if="imageUrl" slot="file" slot-scope="{ file }"> <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" /> <span class="el-upload-list__item-actions"> <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)"> <i class="el-icon-zoom-in"></i> </span> <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)" > <i class="el-icon-download"></i> </span> <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)" > <i class="el-icon-delete"></i> </span> </span> </div> </el-upload> <!-- 图片查看器对话框 --> <el-dialog :visible.sync="dialogVisible"> <img width="100%" :src="dialogImageUrl" alt="" /> </el-dialog> </div> </template> <script> export default { data() { return { fileList: [], // 存储当前选定文件的信息列表 imageUrl: '', // 当前展示的大尺寸图片链接 dialogImageUrl: '', dialogVisible: false, }; }, methods: { handleRemove(file, fileList) { this.$refs.uploadRef.clearFiles(); // 移除所有文件条目 this.fileList = []; // 更新本地状态为空数组 this.imageUrl = ''; // 清理大图URL }, handlePictureCardPreview(file) { this.dialogImageUrl = file.url; this.dialogVisible = true; } } }; </script> ``` 此段代码实现了基本的需求——仅限一张图片的选择、即时预览以及便捷地取消选项。每当用户执行删除动作时,不仅清除了内部缓存中的待传对象集合,同时也重置了界面上可见的状态表示,从而确保界面始终反映最新的交互成果[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值