Element-UI上传组件自定义进度条不使用默认
最终效果
实现步骤
- 首先隐藏
el-upload
组件自带的文件列表和进度:将show-file-list
赋值为false
- 实现
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单独放到一个整体的数组里对用每个条目统一管理。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)