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单独放到一个整体的数组里对用每个条目统一管理。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Element UI 提供了一个名为 `el-upload` 的组件,可以用来实现文件上传功能,并且可以显示上传进度条。下面是一个简单的示例代码: ```html <template> <div> <el-upload class="upload-demo" action="//jsonplaceholder.typicode.com/posts/" :on-progress="handleUploadProgress" :on-success="handleUploadSuccess" :on-error="handleUploadError" :http-request="customHttpRequest" :auto-upload="false" > <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> <el-progress v-if="showProgress" :percentage="uploadProgress"></el-progress> </div> </template> <script> export default { data() { return { uploadProgress: 0, showProgress: false }; }, methods: { handleUploadProgress(event, file, fileList) { this.uploadProgress = event.percent; }, handleUploadSuccess(response, file, fileList) { // 处理上传成功的逻辑 }, handleUploadError(error, file, fileList) { // 处理上传失败的逻辑 }, customHttpRequest(options) { // 自定义上传请求逻辑,可以使用 axios 或其他库发送请求 // 在请求中可以通过 options.onProgress 来获取上传进度并更新到 this.uploadProgress } } }; </script> ``` 在上述示例代码中,通过 `el-upload` 组件实现了文件上传功能。`action` 属性指定了上传文件的地址,`on-progress` 事件监听上传进度,`on-success` 事件监听上传成功,`on-error` 事件监听上传失败。`http-request` 属性可以自定义上传请求逻辑,其中可以通过 `options.onProgress` 获取上传进度并更新到 `this.uploadProgress`。最后,通过 `el-progress` 组件来显示上传进度条,通过控制 `showProgress` 属性来控制进度条的显示与隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@胡海龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值