最终效果
实现步骤
- 首先隐藏
el-upload 组件自带的文件列表和进度:将show-file-list 赋值为false - 实现
on-progress 钩子函数,使用它的自带参数获取上传进度和总文件大小
整体测试代码
<template>
<div class="upload-test">
<div class="upload-item">
<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;
}
.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单独放到一个整体的数组里对用每个条目统一管理。
|