dialogUpload.vue 2.34 KB
Newer Older
何虹's avatar
何虹 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
<template>
  <el-dialog
    title="导入"
    :visible.sync="dialogVisible"
     width="40%"
    :before-close="handleClose"
    :close-on-click-modal='false'
    @open='open'
    :append-to-body='true'

    >
     <el-upload
        class="upload-demo"
        :drag='false'
        ref='uploadEl'
        action="https://jsonplaceholder.typicode.com/posts/"
        :auto-upload="false"
        :on-change="handleChange"
        :accept='accept'
        :multiple='false'
        :on-remove="handleRemove"
        >
        <!-- <i class="el-icon-upload"></i> -->
        <!-- <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div> -->
        <el-button size="small" type="primary">选择文件</el-button>
        <div class="el-upload__tip" slot="tip">只能上传xlsx/xls文件</div>
      </el-upload>
        <span slot="footer" class="dialog-footer">
          <el-button  size='mini' @click="dialogVisible = false">取 消</el-button>
          <el-button size='mini' type="primary" @click="submit">确 定</el-button>
        </span>
  </el-dialog>
</template>
<script>
export default {
  name: 'hsDialogUpload',
  props: {
    value: {
      type: Boolean,
      default: false

    },
    entityConfig: {},
    currentImportItem: {}
  },
  watch: {
    value(val) {
      this.dialogVisible = val
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  data() {
    return {
      dialogVisible: false,
      fileList: [],
      accept: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  },
  methods: {
    handleRemove(file, fileList) {
      if (!fileList.length) {
        this.fileList = []
      }
    },
    handleChange(file, fileList) {
      const uploadFiles = this.$refs.uploadEl.uploadFiles.pop()
      this.$refs.uploadEl.uploadFiles = [uploadFiles]
      const file_ = [file]
      this.fileList = file_
    },
    open() {
      const r = this.$refs.uploadEl
      this.fileList = []
      if (r) { // 清空上传列表
        r.clearFiles()
      }
    },
    handleClose() {
      this.dialogVisible = false
    },
    submit() {
      if (!this.fileList.length) {
        this.$message.error('请选择文件')
        return
      }
      this.$emit('submit', this.fileList)
    }
  }
}
</script>
<style scoped>
.upload-demo{
  text-align: center
}
</style>