Blame view

src/components/upload/FileUpload.vue 2.09 KB
6ec243d6   wuxw   v1.9 点击提交后,成功提示没有...
1
  <!-- src/components/upload/FileUpload.vue -->
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
2
  <template>
27dcfde5   wuxw   系统全面测试完成
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
    <div>
      <el-upload :action="uploadUrl" :headers="headers" :on-success="handleSuccess" :on-error="handleError"
        :show-file-list="false" :before-upload="beforeUpload" name="uploadFile">
        <el-button size="small" type="primary">{{ $t('common.upload') }}</el-button>
        <span v-if="value" style="margin-left: 10px">
          {{ fileName }}
          <el-button type="text" @click.stop="downloadFile">
            <i class="el-icon-download"></i>
          </el-button>
          <el-button type="text" @click.stop="removeFile">
            <i class="el-icon-delete"></i>
          </el-button>
        </span>
      </el-upload>
    </div>
  </template>
  
  <script>
  import { getHeader } from '@/utils/header'
  export default {
    name: 'FileUpload',
    props: {
      value: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        uploadUrl: '/upload/uploadFile'
      }
    },
    computed: {
      fileName() {
        return this.value ? this.value.split('/').pop() : ''
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
38
      },
27dcfde5   wuxw   系统全面测试完成
39
40
41
42
43
44
45
46
47
48
49
      headers() {
        return getHeader()
      }
    },
    methods: {
      handleSuccess(response, file) {
        console.log('Upload file:', file)
        if (response.code == 0) {
          const fileUrl = response.url || response.path
          this.$emit('input', fileUrl)
          this.$emit('success', fileUrl)
6ec243d6   wuxw   v1.9 点击提交后,成功提示没有...
50
          this.$message.success(this.$t('common.operationSuccess'))
27dcfde5   wuxw   系统全面测试完成
51
52
        } else {
          this.$message.error(response.msg || this.$t('upload.error'))
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
53
54
        }
      },
27dcfde5   wuxw   系统全面测试完成
55
56
57
      handleError(err) {
        console.error('Upload error:', err)
        this.$message.error(this.$t('upload.error'))
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
58
      },
27dcfde5   wuxw   系统全面测试完成
59
60
61
62
      beforeUpload(file) {
        const isLt10M = file.size / 1024 / 1024 < 10
        if (!isLt10M) {
          this.$message.error(this.$t('upload.sizeLimit'))
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
63
        }
27dcfde5   wuxw   系统全面测试完成
64
65
66
67
68
69
70
71
72
73
74
75
        return isLt10M
      },
      removeFile() {
        this.$confirm(this.$t('common.confirmDelete'), this.$t('common.tip'), {
          type: 'warning'
        }).then(() => {
          this.$emit('input', '')
          this.$emit('remove')
        }).catch(() => { })
      },
      downloadFile() {
        window.open(this.value, '_blank')
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
76
77
      }
    }
27dcfde5   wuxw   系统全面测试完成
78
79
  }
  </script>