文件的手动上传


一、vue文件的手动上传

1.html结构

代码如下(示例):

 <!-- 批量导出弹窗 -->
    <el-dialog
      class="inputBatch"
      title="批量导入"
      :visible.sync="showInputMany"
      width="35%"
      :before-close="handleDialogClose"
      :lockScroll="false"
    >
      <div class="outsiderWrap">
        <el-upload
          class="upload-demo"
          ref="upload"
          :on-change="onChange"
          action="#"
          :limit="1"
          :file-list="fileList"
          :on-remove="handleRemove"
          :auto-upload="false"
        >
          <el-button
            v-if="!canUpload"
            :disabled="!canUpload"
            type="primary"
            plain
            class="btn"
            slot="tip"
          >
            <img
              class="button_icon"
              src="@/assets/common/icon_Line_Upload.svg"
              alt=""
            />
            上传文件
          </el-button>
          <el-button
            v-if="canUpload"
            type="primary"
            plain
            class="btn"
            slot="trigger"
          >
            <img
              class="button_icon"
              src="@/assets/common/icon_Line_Upload.svg"
              alt=""
            />
            上传文件
          </el-button>
        </el-upload>
      </div>
      <a class="inputModel" href="javascript:;" @click="getModel"
        ><img
          src="@/assets/common/icon_template_download.svg"
          alt=""
        />获取导入模板</a
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="canCelInput">取 消</el-button>
        <el-button type="primary" @click="confirmSubmit">确 定</el-button>
      </span>
    </el-dialog>

2.js代码

代码如下(示例):

export default {
data(){
return {
	fileList:[],
	canUpload: true,
   }
},
methods:{
onChange(file, fileList) {
      let Name = file.name;
      let fileName = Name.toLowerCase().substr(Name.lastIndexOf("."));
      console.log("file.size", file);
      const isLt2M = file.size > 31457280;
      if (fileName != ".xls" && fileName != ".xlsx") {
        this.fileList = [];
        return this.$message.error("仅支持扩展名 .xls .xlsx !");
      }
      if (isLt2M) {
        this.fileList = [];
        return this.$message.error("上传文件大小不得超过30M!");
      }
      this.fileList = fileList;
    },
    handleRemove() {
      this.fileList = [];
    },
    confirmSubmit() {
      if (this.fileList.length === 0)
        return this.$message.error("请上传文件!");
      this.uploadSectionFile();
    },
     async uploadSectionFile() {
      const loading = this.$loading({
        lock: true,
        text: "上传中...",
        spinner: "el-icon-loading",
        background: "rgba(7, 17, 27, 0.3)",
        customClass: "myLoading",
      });
      const FormDataFile = new FormData();
      const file = this.fileList[0].raw;
      FormDataFile.append("file", file);
      const res = await importBatch(FormDataFile);
      clearTimeout(this.timer);
      this.timer = null;
      this.timer = setTimeout(() => {
        if (res.code == 200) {
          loading.close();
          this.$message.success("导入成功");
          this.canCelInput();
          this.queryInfo.pageNum = 1;
          this.getDataList();
        } else if (res.code == 70050) {
          loading.close();
          this.$confirm(res.msg, "数据重复", {
            // confirmButtonText: "确定",
            // cancelButtonText: "取消",
            type: "warning",
            lockScroll: true,
            customClass: "myClass",
            center: true,
          })
            .then()
            .catch((err) => {
              // console.log(err);
            });
        } else {
          loading.close();
          this.$message.error(res.msg);
        }
      }, 500);
    },
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值