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

<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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为AI语言模型,我无法上传图片。但是,我可以为您提供一些关于el-upload上传图片的帮助。 el-uploadElement UI提供的一个上传组件,可以用来上传图片、文件等。以下是一些基本用法: 1.安装el-upload组件: ``` npm install element-ui --save ``` 2.引入el-upload组件: ``` import { Upload } from 'element-ui'; ``` 3.使用el-upload组件: ```html <template> <el-upload class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :on-remove="handleRemove" :before-upload="beforeUpload" :on-success="handleSuccess" :on-error="handleError" :limit="3" :on-exceed="handleExceed" :file-list="fileList"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </template> <script> export default { data() { return { fileList: [] }; }, methods: { handlePreview(file) { console.log('预览文件', file); }, handleRemove(file, fileList) { console.log('移除文件', file, fileList); }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt500K = file.size / 1024 < 500; if (!isJPG) { this.$message.error('上传图片只能是 JPG/PNG 格式!'); return false; } if (!isLt500K) { this.$message.error('上传图片大小不能超过 500KB!'); return false; } return true; }, handleSuccess(response, file, fileList) { console.log('上传成功', response, file, fileList); }, handleError(error, file, fileList) { console.log('上传失败', error, file, fileList); }, handleExceed(files, fileList) { this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`); } } }; </script> ``` 这段代码中,我们创建了一个简单的上传组件,可以上传图片,同时设置了一些限制,如只能上传jpg/png文件,且不超过500kb,最多上传3个文件等。 希望这能帮到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值