vue+element上传文件进度条

先上传文件,第一个接口会返回一个文件id,用文件id调用第二个接口,会返回文件进度。
200毫秒轮询一次,当进度接口返回100时,取消轮询。

  <div class="flex-wrap">
            <i
              class="iconfont icon-wenjianshangchuan gc-ft-shallowGray"
              :class="isUp ? 'green' : ''"
            ></i>
            <p class="tip">2.上传填写好的文件,仅支持XLS、XLSX格式文件</p>
            <p class="txt" v-if="isUp">{{ fileName }}</p>
            <p class="txt">
              <el-progress
                v-if="isImport"
                :percentage="percentage"
                color="#61C5C1"
              ></el-progress>
            </p>

            <el-button
              v-if="!isImport"
              slot="trigger"
              @click="creatiptFile"
              :class="isUp ? 'updata' : ''"
              >{{ isUp ? "重新上传" : "上传文件" }}</el-button
            >
            <el-button
              v-if="isUp && !isImport"
              type="success"
              @click="uploadFile"
              >确认导入</el-button
            >
            <el-button v-if="isImport" :loading="true" type="success"
              >导入中</el-button
            >
          </div>
data() {
    return {
      header_title: "批量添加",
      isUp: false,
      isImport: false,
      fileName: "",
      percentage: 0,
    };
  },
methods: {
  creatiptFile() {
      this.percentage = 0;
      let _this = this;
      let input = document.createElement("input");
      input.type = "file";
      input.accept = ".xls,.xlsx";
      input.click();

      input.onchange = function (res) {
        console.log(input.files);
        _this.isUp = true;
        _this.fileName = input.files[0].name;
        _this.fileObj = input.files;
      };
    },
    uploadFile() {
      let that = this;
      let form = new FormData(); // FormData 对象
      let blob = new Blob([this.fileObj[0]]);
      form.append("file", blob, this.fileObj[0].name);
      this.isImport = true;
      this.isUp = false;
      if (that.isImport) that.getimport(form);
    },
      getimport(form) {
      let that = this;
      httpApi
        .import(form)
        .then((res) => {
          if (res.code == 200) {
            let config = {
              product_id: res.data.product_id,
            };
            that.getSchedule(config);
          } else {
            this.isImport = false;
          //失败
          }
        })
        .catch((err) => {
          that.isImport = false;
          this.$message.info(err.message);
        });
    },
    getSchedule(config) {
      let that = this;
      httpImport.getSchedule(config).then((res) => {
        if (res.code == 200) {
          this.percentage = res.data.percent;
          if (res.data.percent == 100) {
            setTimeout(function () {
              that.isImport = false;
            }, 200);
          } else {
            setTimeout(function () {
              if (that.isImport) that.getSchedule(config);
            }, 500);
          }
        } else {
          console.log("上传成功")
        }
      });
    },
  

效果如下
在这里插入图片描述

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值