vue2+上传文件

<template>
  <ElDialog
    :width="'600px'"
    :title="$t('common.channel')"
    :modelVisible="visible"
    @handleConfirm="handleConfirm"
    @handleClose="handleClose"
    :loading="loading"
    :isFooter="true"
  >
    <div class="upload-content">
      <el-upload
        class="upload-demo"
        ref="uploader"
        drag
        accept=".xlsx, .csv"
        :format="['xlsx', 'xls', 'csv']"
        :on-format-error="handleFormatError"
        :action="host + 'hsn/uploads'"
        :file-list="fileList"
        :on-change="changeFile1"
        :on-success="onFileUploadSuccess"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text" style="color: #fff">
          {{ $t('common.import.text') }}
          <div style="color: skyblue">{{ $t('common.import.text1') }}</div>
        </div>
      </el-upload>
    </div>
  </ElDialog>
</template>

<script>
import ElDialog from '@/components/ElDialog.vue'

import { import_areas } from '@/graphqls/common.js'
export default {
  name: 'Index',
  components: {
    ElDialog
  },
  props: {
    dialogTitle: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      visible: false,
      host: this.$TOOL.baseURL,
      path: '',
      fileList: [],
      loading: false
    }
  },
  watch: {
    dialogTitle: {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.dialogTitles = newVal // 使用对象解构复制确保响应式
        }
      }
    }
  },
  mounted() {},
  methods: {
    // 弹窗打开
    open() {
      this.visible = true
      this.fileList = []
    },
    // 取消
    handleClose() {
      this.visible = false
    },
    // 确定
    async handleConfirm() {
      if (this.path) {
        if (
          this.path.indexOf('.xls') != -1 ||
          this.path.indexOf('.xlsx') != -1 ||
          this.path.indexOf('.csv') != -1
        ) {
          this.loading = true
          this.$API.auth.common
            .post({
              operationName: 'import_areas',
              variables: {
                path: this.path
              },
              query: import_areas
            })
            .then(({ data }) => {
              this.loading = false
              let imData = data.import_areas
              if (imData) {
                // let msg = `成功${imData.success}条,失败${imData.fail}条,失败原因:${imData.message}`

                if (imData.fail == 0 && imData.success != 0) {
                  this.$notify({
                    type: 'success',
                    duration: 5000,
                    title: this.$t('common.tishi'),
                    message: this.$t('common.importS')
                  })
                  this.path = null
                  this.getList()
                } else if (imData.fail == 0 && imData.success == 0) {
                  this.$notify({
                    type: 'error',
                    duration: 5000,
                    title: this.$t('common.tishi'),
                    message: this.$t('common.importFail')
                  })
                } else {
                  this.$notify({
                    type: 'warning',
                    duration: 5000,
                    title: this.$t('common.tishi'),
                    message: this.$t('common.importFail')
                  })
                }
              } else {
                this.$message.error(this.$t('common.importFail'))
              }
              this.path = null
              this.uploadLoading = false
              this.showUpload = false
            })
            .catch((err) => {
              this.uploadLoading = false
            })
        } else {
          this.$message.warning(this.$t('common.import.text3'))
        }
      } else {
        this.$message.warning(this.$t('common.import.text4'))
      }
    },
    handleFormatError(file) {
      console.log(file, 'file')
      this.$message.warning({
        title: '文件类型错误',
        duration: 5,
        desc:
          '文件:' +
          file.name +
          '文件格式不对,请选择后缀为.xlsx或者.xls、.csv文件。'
      })
    },
    changeFile1(file, fileList) {
      if (fileList.length > 1) {
        fileList.splice(0, 1)
      }
      this.fileList = fileList
    },
    onFileUploadSuccess(response, file, fileList) {
      console.log(response.response.id)
      this.path = response.response.id
    }
  }
}
</script>
<style lang="less" scoped>
.upload-content {
  clear: both;
  overflow: hidden;
  height: 220px;
  display: flex;
  margin-bottom: 30px;
  justify-content: space-around;
}
/deep/.el-upload-dragger {
  background-color: transparent !important;
}
</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值