Element-UI上传组件自定义进度条不使用默认

最终效果

在这里插入图片描述

实现步骤

  1. 首先隐藏el-upload组件自带的文件列表和进度:将show-file-list赋值为false
  2. 实现on-progress钩子函数,使用它的自带参数获取上传进度和总文件大小

整体测试代码

<template>
    <div class="upload-test">
        <div class="upload-item">
            <!-- <div class="file_name">测试上传文件</div> -->
            <el-upload :show-file-list="false" :action="`${uploadUrl}doc/doc-file/upload`" :headers="header" :on-success="handleSuccess" :on-progress="handleProgress">
                <el-button size="small" type="info">上传文件</el-button>
            </el-upload>
            <div class="progress">
                <el-progress :percentage="percent"></el-progress>
            </div>
            <div class="total" v-if="isSuccess">
                {{total/1024}}KB
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'UploadTest',
    data(){
        return{
            uploadUrl: window._apiUrl.webUrl,
            header: {
                Authorization: localStorage.getItem("token"),
            },
            percent: 0,
            total: 0,
            isSuccess: false
        }
    },
    methods:{
        handleSuccess(res){
            this.$message({
                type: 'success',
                message: res.message
            })
            this.isSuccess = true
        },
        handleProgress(event){
            console.log(event)
            this.percent = event.percent
            this.total = event.total
        }
    }
}
</script>
<style scoped>
.upload-test{
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
.upload-item{
    width: 80%;
    background-color: #eeeeee;
    padding: 10px;
    display: flex;
    align-items: center;
    /* justify-content: space-around; */
}
.progress{
    width: 30%;
    margin-left: 10px;
}
.file_name{
    width: 30%;
    text-align: left;
}
.status{
    width: 10%;
}
.total{
    padding-left: 20px;
    font-size:13px;
}
</style>

如果是多条upload-item时需要将每条上传的percent和total单独放到一个整体的数组里对用每个条目统一管理。

posted @   胡海龙  阅读(529)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
www.huhailong.vip
点击右上角即可分享
微信分享提示