先下载模版表格,再上传表格(element-ui弹窗组件)

该文章展示了一个使用Element-UI框架的弹窗组件来处理Excel文件上传的过程。用户先下载模板,然后通过对话框中的输入框选择文件进行上传。代码中包含了处理文件上传的函数以及调用API接口进行数据交互的部分。
摘要由CSDN通过智能技术生成

先下载模版表格,再上传表格(element-ui弹窗组件)

<template>
  <el-dialog title="员工上传" width="500px" :visible="showExcelDidlog" @close="hClose">
    <el-row type="flex" justify="center">
      <div class="upload-excel">
        <input
          ref="excel-upload-input"
          class="excel-upload-input"
          type="file"
          accept=".xlsx"
          @change="uploadChange"
        >
        <div class="drop">
          <i class="el-icon-upload" />
          <el-button type="text" @click="hDownLoadExcel">下载导入模板</el-button>
          <span>将文件拖到此处或
            <!-- <el-upload
              action="https://jsonplaceholder.typicode.com/posts/"
              accept=".xlsx, .xls"
              style="display: inline-block;"
            > -->
            <el-button type="text" @click="handleUpload">点击上传</el-button>
            <!-- </el-upload> -->
          </span>
        </div>
      </div>
    </el-row>
    <el-row type="flex" justify="end">
      <!-- update:props属性名,值 直接修改 .sync修饰符的属性值 -->
      <el-button size="mini" type="primary" @click="hClose">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getExportTemplate, uploadExcel } from '@/api/employee'
import FileSaver from 'file-saver'
export default {
  props: {
    showExcelDidlog: {
      type: Boolean,
      required: true
    }
  },
  methods: {
    hClose() {
      this.$emit('update:showExcelDidlog', false)
    },
    async hDownLoadExcel() {
      // 下载空的模版
      const res = await getExportTemplate()
      console.log(res)
      FileSaver.saveAs(res, '员工信息表(模版).xlsx')
    },
    handleUpload() {
      this.$refs['excel-upload-input'].click()
      // this.$refs.属性名 和 this.$refs[属性名] 等价
    },
      //重点
    async uploadChange(e) {
      console.log('uploadChange', '上传')
      const file = e.target.files[0]
      console.log(file)
      if (file) {
        const fd = new FormData()
        fd.append('file', file)
        await uploadExcel(fd)  //上传api
        this.$message.success('操作成功')
      }
    }
      //重点
  }
}
</script>

<style scoped lang="scss">
    .upload-excel {
      display: flex;
      justify-content: center;
      margin: 20px;
      width: 360px;
      height: 180px;
      align-items: center;
      color: #697086;
      .excel-upload-input {
        display: none;
        z-index: -9999;
      }
      .btn-upload,
      .drop {
        border: 1px dashed #dcdfe6;
        width: 100%;
        height: 100%;
        text-align: center;
        line-height: 160px;
        border-radius: 8px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
      .drop {
        line-height: 40px;
        color: #bbb;
        i {
          font-size: 60px;
          display: block;
          color: #c0c4cc;
        }
      }
    }
</style>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值