UploadFz.vue
4.33 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
<div class="UploadFile-container">
<el-upload :action="define.comUploadUrl+'/'+type" :headers="uploadHeaders" ref="elUpload"
:on-success="handleSuccess" :multiple="limit!==1" :show-file-list="false" :accept="accept"
:before-upload="beforeUpload" :on-exceed="handleExceed" :disabled="disabled" :limit="limit" v-if="!disabled">
<el-button size="small" icon="el-icon-upload" v-if="!disabled" :disabled="disabled" :on-preview="handlePreview">{{buttonText}}</el-button>
<div slot="tip" class="el-upload__tip" v-show="showTip">
只能上传不超过{{fileSize}}{{sizeUnit}}的{{accept}}文件
</div>
</el-upload>
<template v-if="fileList.length">
<transition-group class="el-upload-list el-upload-list el-upload-list--text" tag="ul"
name="el-list">
<li class="el-upload-list__item is-success" v-for="(file,index) in fileList"
:key="file.fileId">
<el-tooltip class="item" effect="dark" placement="left">
<div slot="content">
<img class="avatar" :src="define.comUrl + file.url" style="max-width: 400px; max-height: 400px;">
</div>
<a class="el-upload-list__item-name" @click="handleClick(file)" style="flex: 1;">
<i class="el-icon-download"></i>{{file.name}}
</a>
</el-tooltip>
<label class="el-upload-list__item-status-label">
<i class="el-icon-upload-success el-icon-circle-check"></i>
</label>
<i class="el-icon-close" v-if="!disabled" @click="handleRemove(file,index)"></i>
</li>
</transition-group>
</template>
</div>
</template>
<script>
const units = {
KB: 1024,
MB: 1024 * 1024,
GB: 1024 * 1024 * 1024
}
import { getDownloadUrl } from '@/api/common'
export default {
name: 'UploadFile',
props: {
value: {
type: Array,
default: () => []
},
type: {
type: String,
default: 'annex'
},
disabled: {
type: Boolean,
default: false
},
showTip: {
type: Boolean,
default: false
},
limit: {
type: Number,
default: 0
},
accept: {
type: String,
default: '*'
},
buttonText: {
type: String,
default: '选择文件'
},
sizeUnit: {
type: String,
default: 'MB'
},
fileSize: {
default: 50
},
},
data() {
return {
fileList: this.value,
uploadHeaders: { Authorization: this.$store.getters.token }
}
},
watch: {
value: {
immediate: true,
handler(val) {
this.fileList = val
}
}
},
methods: {
beforeUpload(file) {
const unitNum = units[this.sizeUnit];
if (!this.fileSize) return true
let isRightSize = file.size / unitNum < this.fileSize
if (!isRightSize) {
this.$message.error(`文件大小超过${this.fileSize}${this.sizeUnit}`)
}
// let isAccept = new RegExp(this.accept).test(file.type)
// if (!isAccept) {
// this.$message.error(`应该选择${this.accept}类型的文件`)
// }
return isRightSize;
},
handleSuccess(res, file, fileList) {
if (res.code == 200) {
this.fileList.push({
name: file.name,
fileId: res.data.name,
url: res.data.url
})
this.$emit('input', this.fileList)
} else {
fileList.filter(o => o.uid != file.uid)
this.$emit('input', this.fileList)
this.$message({ message: res.msg, type: 'error', duration: 1500 })
}
},
handleExceed(files, fileList) {
this.$message.warning(`当前限制最多可以上传${this.limit}个文件`);
},
handlePreview(file) {
// console.log(file);
},
handleRemove(file, index) {
this.fileList.splice(index, 1)
this.$refs.elUpload.uploadFiles.splice(index, 1)
this.$emit("input", this.fileList)
// this.$confirm(`确定移除${file.name}?`, '提示').then(() => {
// }).catch(() => { })
},
handleChaeck(file, index) {
window.open(this.define.comUrl + file.url, '_blank')
},
handleClick(file) {
// 点击下载文件
if (!file.fileId) return
getDownloadUrl(this.type, file.fileId, file.name).then(res => {
if (res.data.url) window.location.href = this.define.comUrl + res.data.url
})
}
}
}
</script>