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
// 单文件上传及展示
<template>
<div class="my-upload">
<label>
<el-button icon="el-icon-upload" size="mini" :loading="loading">{{
loading ? "上传中..." : tip
}}</el-button>
<input
type="file"
@change="chooseFile"
:disabled="loading"
ref="referenceUpload"
/>
</label>
<div v-if="value" class="flex">
<div style="margin-right: 20px">
<div v-if="type === 'thumb'" class="thumb" :style="imgCover"></div>
<div v-else>文件名: {{ value }}</div>
</div>
<!-- <div>
<el-button icon="el-icon-delete" size='mini' plain @click='del'>删除</el-button>
</div>-->
</div>
</div>
</template>
<script>
export default {
props: {
value: {
type: String,
default: "",
},
type: {
// 展示类型,文件名或缩略图 filename | thumb
type: String,
default: "filename",
},
tip: {
// 按钮文字
type: String,
default: "上传文件",
},
prePath: {
type: String,
default: "",
},
},
methods: {
del() {
this.$emit("input", "");
},
async chooseFile(e) {
this.loading = true;
try {
const { code, url } = await this.$upload("file/upload", {
uploadFile: e.target.files[0],
prePath: this.prePath,
});
if (code === 1) {
this.$emit("input", url);
} else {
this.$message.error("上传文件失败");
}
} catch (error) {
this.$message.error(error.message);
}
this.loading = false;
this.$refs.referenceUpload.value = null;
},
},
computed: {
imgCover() {
return `background-image: url(${this.value})`;
},
},
data() {
return {
loading: false,
};
},
};
</script>
<style lang="less">
.my-upload {
label {
position: relative;
input {
display: block;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
opacity: 0;
}
}
.thumb {
width: 80px;
height: 80px;
background-size: contain;
background-position: center center;
background-repeat: no-repeat;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
}
p {
color: #666;
font-size: 14px;
}
}
</style>