Blame view

src/components/upload/FileUpload.vue 2.28 KB
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
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
  <!-- src/components/upload/FileUpload.vue -->
  <template>
      <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() : ''
        },
        headers() {
          return getHeader()
        }
      },
      methods: {
        handleSuccess(response, file) {
          console.log('Upload file:', file)
          if (response.code == 0) {
48ea9c43   wuxw   巡检开发完成
54
            const fileUrl = response.url || response.path
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
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
            this.$emit('input', fileUrl)
            this.$emit('success', fileUrl)
            this.$message.success(this.$t('upload.success'))
          } else {
            this.$message.error(response.msg || this.$t('upload.error'))
          }
        },
        handleError(err) {
          console.error('Upload error:', err)
          this.$message.error(this.$t('upload.error'))
        },
        beforeUpload(file) {
          const isLt10M = file.size / 1024 / 1024 < 10
          if (!isLt10M) {
            this.$message.error(this.$t('upload.sizeLimit'))
          }
          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')
        }
      }
    }
    </script>