index.vue
1.74 KB
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
<style>
.eagle-file-icon {
color: #8c8c8c;
}
.eagle-file-link {
color: #2f54eb;
display: flex;
align-items: center;
}
.eagle-file-name {
padding: 0px 15px;
}
.eagle-file-name:hover {
color: #69c0ff;
}
.eagle-file-delete {
color: #d9d9d9;
}
.eagle-file-delete:hover {
color: #f5222d;
}
</style>
<template>
<el-upload
:action="url"
:show-file-list="false"
:headers="headers"
:data="param"
:on-success="handleSuccess"
:before-upload="beforeUpload"
style="width: 100%;"
>
<span v-if="value" class="eagle-file-link" @click.stop>
<i class="iconfont icon-attachment eagle-file-icon"></i>
<a class="eagle-file-name" :href="value" target="_blank">点击下载</a>
<i class="iconfont icon-close eagle-file-delete" title="点击删除" @click="deleteFile"></i>
</span>
<el-button v-else size="small" type="primary">点击上传</el-button>
</el-upload>
</template>
<script>
export default {
props: {
headers: {
type: Object,
default() {
return {}
}
},
url: {
type: String,
required: true
},
value: String,
param: {
type: Object,
default() {
return {};
}
},
maxMB: {
type: Number,
default: 2
}
},
name: 'FileUpload',
methods: {
handleSuccess(response = {}, file, fileList) {
const { result = [] } = response
const url = result[0]
this.$emit('input', url)
},
beforeUpload(file) {
const isLtMaxMB = file.size / 1024 / 1024 < this.maxMB
if (!isLtMaxMB) {
this.$message.error(`上传附件大小不能超过 ${this.maxMB}MB!`)
}
return isLtMaxMB
},
deleteFile() {
this.$emit('input', undefined)
}
}
}
</script>