el-upload多文件上传进度条实现(非原生真实进度)

 先上效果图

 

 

<el-upload
      :disabled="disableUpload"
       class="upload-demo"
       drag
       action="#"
       :show-file-list="false"
       :file-list="fileList"
       :http-request="httpRequest"
       multiple
       :on-change="handleChange"
       >
       <i class="el-icon-upload"></i>
           <div class="el-upload__text">
                将文件拖到此处,或<em v-if="!disableUpload">点击上传</em
                ><em v-else>上传中... ,请勿进行其他操作</em>
           </div>
 </el-upload>
data(){
  return{
      fileList: [],
      percentage: 0,
      percentNow: 0,
      percentageTotal: 0,
      disableUpload: false,

       }
    } 
  watch: {//解决弹窗比进度条快的问题
    percentage(newval, old) {
      if (newval == 100) {
        setTimeout(() => {
          this.loading = false;
          this.$message({
            message: "上传成功",
            type: "success",
          });
          this.fileList = []
          this.percentNow = 0
          this.disableUpload = false

        }, 1000);

      }
    }
  },

自定义上传方法 

 async httpRequest(data) {
      if (this.fileList.length == this.percentageTotal) {
        this.percentage = 0
        try {
          this.loading = true;
          let result = await this.uploadFileApi({
            file: data.file,
          })
           this.percentNow+=1
          if (result.data.code == 0) {
            this.percentage = parseInt((this.percentNow / this.percentageTotal) * 100)
          }//实际为当前已上传文件占总文件的百分比

        } catch (error) {
          this.loading = false;
          this.$message.error("上传失败!");
        }

      }

    },
//获取文件列表 上传中禁止再次上传
 handleChange(file, fileList) {
      this.fileList = fileList
      this.disableUpload = true
    },

上传方法 

  uploadFileApi(data) {
      var formData = new FormData();
      for (let key in data) {
        formData.append(key, data[key]);
      }
      return axios({
        url: this.$store.state.staticUrl + '/api/web/uploadFile',
        method: 'post',
        header: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
        // onUploadProgress: (progressEvent) => {
        //   if (progressEvent.lengthComputable) {
        //     let complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0;

        //     this.updateProgress(complete)
        //   }//此处为真实上传进度回调 但本地调试会跨域 nginx已经开启跨域的情况下也是
        // },
        data: formData,
      })
    },

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-upload 是 Element UI 提供的文件组件,支持多文件。如果需要为 el-upload 组件添加多文件进度条,可以使用 el-progress 组件结合 el-upload 的 events 属性实现。 首先,在 el-upload 组件中添加 `show-file-list="false"` 属性,隐藏上文件列表。然后,在 el-upload 组件中添加一个 el-progress 组件,将 el-progress 组件的 `percentage` 属性绑定到一个变量(如 `uploadPercent`),并设置 el-progress 组件的 `stroke-width` 属性和 `status` 属性。最后,在 el-upload 组件的 `before-upload` 和 `on-progress` 事件中更新 `uploadPercent` 变量的值。 以下是示例代码: ```html <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :on-error="handleError" :before-upload="beforeUpload" :on-progress="onProgress" :show-file-list="false"> <el-button size="small" type="primary">点击上</el-button> <el-progress :percentage="uploadPercent" :stroke-width="8" status="success"></el-progress> </el-upload> ``` ```javascript data() { return { uploadPercent: 0 } }, methods: { beforeUpload(file) { // 在上前重置上进度条 this.uploadPercent = 0 }, onProgress(event, file, fileList) { // 更新上进度条的值 this.uploadPercent = event.percent }, handleSuccess(response, file, fileList) { // 上成功后重置上进度条 this.uploadPercent = 0 }, handleError(error, file, fileList) { // 上失败后重置上进度条 this.uploadPercent = 0 } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值