实现上传文件进度条(vue+axios+element-ui)

这篇博客介绍了如何在Vue项目中实现文件上传功能,包括限制文件类型、设置上传状态和显示上传进度。通过使用`el-upload`组件和`axios`处理HTTP请求,配合`el-progress`组件实时更新上传进度条。示例代码详细展示了文件上传前的验证、 formData 创建、axios的onUploadProgress监听以及进度条更新的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相关组件

// 上传组件
```javascript
<el-upload class="upload-demo" :before-upload="beforeUpload">
  <el-button size="small" type="primary">点击上传</el-button>
</el-upload>
// 进度条组件
<el-progress :percentage="progress "></el-progress>

<script>
...
methods:{
	beforeUpload(file) {
       const type = file.file.name.slice(-3);
       if (!this.fileType.includes(type)) {
            this.$message('文件类型不支持');
            return;
       }
       this.uploadStatus = 1;
       this.progress = 0;
       const formData = new window.FormData();
       const that = this;
       formData.append('file', file.file);
       axios({
          url: 'xxx',
          method: 'POST',
           data: formData,
           // eslint-disable-next-line eqeqeq
           onUploadProgress: (progressEvent) => {
           // eslint-disable-next-line
            let complete = (progressEvent.loaded / progressEvent.total * 100 | 0)
            // 这里为上传的进度
           that.progress = complete;
           },
           }).then((res) => {...});
        },
}

</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值