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
<template>
<div>
<el-upload
class="upload-file"
:action="action"
:on-remove="handleRemove"
:before-remove="beforeRemove"
:multiple="true"
:limit="limit"
:data="dataParam"
:on-exceed="handleExceed"
:file-list="dataFileList"
:on-success="handleSuccess"
:on-preview="handlePreview"
>
<el-button size="small" type="primary">点击上传</el-button>
</el-upload>
</div>
</template>
<script>
import { getToken } from "@/utils/auth";
export default {
name: "upFile",
props:{
action:{
type:String,
default:function(){
return ""
}
},
multiple:{
type:Boolean,
default:function(){
return false
}
},
limit:{
type:Number,
default:function(){
return 1
}
},
value:{
type:Array,
default:function(){
return []
}
},
dataParam:{
type:Object,
default:function(){
return {
token:getToken()
}
}
}
},
computed:{
dataFileList:{
get(){
return this.value?this.value:[]
},
set(data){
this.$emit('setUpFielList',data)
},
}
},
methods: {
handleRemove(file, fileList) {
this.setDataFileList(fileList)
},
handleExceed(files, fileList) {
this.$message.warning(
`最多可上传 ${this.limit} 个文件,已超出最大限制数。`
);
},
beforeRemove(file, fileList) {
return this.$confirm(`确定移除 ${file.name}?`);
},
handleSuccess(res,file,fileList){
if(res.code!==0){
fileList.splice(fileList.findIndex(item => item.uid === file.id), 1)
this.msgError(res.msg)
return false
}
file.url = this.apiUrl + "/" + res.data.fileInfo.fileUrl;
this.setDataFileList(fileList)
},
handlePreview(file){
window.open(file.url)
},
setDataFileList(fileList){
let list = [];
fileList.forEach(item => {
list.push({name:item.name,url:item.url})
});
this.dataFileList = list
}
},
};
</script>
<style scoped>
ul.el-upload-list li {
padding-left: 8px;
height: 45px;
line-height: 45px;
background: #efefef;
}
</style>