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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<template>
<div class="upload-layout">
<div v-if="file.downloadUrl" class="file-lay">
<div>
<document-view :fileUrl="file.downloadUrl" :fileName="file.fileName" :imageArray="[file.downloadUrl]"></document-view>
</div>
<a-icon type="delete" class="hover-pointer d-icon" @click="deletefile(file)" style="margin: 0px 2px;" />
</div>
<div v-else>
<a-form-model-item :prop="name+'downloadUrl'" :rules="{required: isRequired, message: message,trigger: 'blur',}">
<input type="file" ref="fileElem" class="visually-hidden" @change="handleFiles">
<a-button @click="fileSelect">
<a-icon type="upload" />选择文件
</a-button>
</a-form-model-item>
</div>
</div>
</template>
<script>
const File = { fileName: null, downloadUrl: null, fileExplain: null, downloadId: null }
import documentView from '@/views/components/common/documentView'
export default {
name: "FileUpload",
components: {
documentView
},
data () {
return {
};
},
props: {
file: {
type: Object,
default: () => {
return { ...File }
}
},
isUpload: {
type: Boolean,
default () {
return false
}
},
isRequired: {
type: Boolean,
default () {
return true
}
},
format: {
type: Array,
default () {
return []
}
},
name: {
type: String,
default () {
return 'fileobj'
}
},
message: {
type: String,
default () {
return '请上传附件'
}
},
},
created () {
},
methods: {
fileSelect () {
let fileElem = this.$refs['fileElem']
if (fileElem) {
fileElem.click()
}
},
handleFiles () {
let fileElem = this.$refs['fileElem']
let files = fileElem.files
if (files.length <= 0) {
this.$message.error('未选中文件,请尝试重新选择')
return
}
if (!this.fileCheck(files[0]))
return
if (this.isUpload) {
this.$api.base.asyncUpload(this.uploadHandle(files[0], files[0].name)).then(({ data = {} }) => {
if (data) {
this.file.fileName = files[0].name
this.file.downloadId = data.id
this.file.downloadUrl = data.downloadUrl
} else
this.$message.error('上传失败')
}).catch(() => {
this.$message.error('上传失败')
})
} else {
this.file.fileName = files[0].name
this.file.downloadUrl = '/' + files[0].name
this.$emit("beforeUpload", files[0]);
}
},
fileCheck (file) {
//判断是否小于1M
let isLtSize = file.size < 1024 * 1024 * 15;
if (!isLtSize) {
this.$message.error('文件大小不能超过15M!');
return false
}
var fileNames = file.name.split('.')
var fileType = fileNames[fileNames.length - 1].toLocaleLowerCase()
if (!!this.format && this.format.length > 0) {
var extList = this.format
if (!extList.find((item) => item == fileType)) {
this.$message.error('文件格式错误!')
return false
}
}
return true
},
deletefile () {
this.$emit("cancel");
if (this.isUpload) {
this.$api.base.deletefile({ id: this.file.downloadId }).then(({ data = {} }) => {
if (data) {
this.file.fileName = null
this.file.downloadUrl = null
this.file.downloadId = null
}
}).catch(() => {
this.$message.error('删除失败')
})
} else {
this.file.fileName = null
this.file.downloadUrl = null
this.file.downloadId = null
}
},
uploadHandle (file, fileName) {
let formData = new FormData()
formData.append('file', file)
formData.append('fileName', fileName)
return formData
},
downloadfile () {
},
},
watch: {
value: {
handler (value) {
},
}
}
};
</script>
<style scoped lang="less">
.upload-layout {
width: 100%;
.file-lay {
display: flex;
justify-content: center;
align-items: center;
margin: 0 8px;
> div:nth-child(1) {
max-width: calc(100% - 0px);
}
}
.visually-hidden {
display: none !important;
}
}
</style>