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
| <template>
| <div>
| <el-upload
| :action="uploadUrl"
| :headers="uploadHeaders"
| :show-file-list="false"
| :on-success="handleUploadSuccess"
| :before-upload="beforeUpload">
| <img v-if="fileUrl" :src="fileUrl" :style="getImgStyle()" class="avatar">
| <i v-else :style="getImgUploadIconStyle()" class="el-icon-plus avatar-uploader-icon"></i>
| </el-upload>
| </div>
| </template>
|
| <script>
| export default {
| name: "UploadImg",
| props: {
| fileSizeLimitM: {
| required: false,
| default: 1,
| type: Number
| },
| imgWidth: {
| required: false,
| default: 178,
| type: Number
| },
| imgHeight: {
| required: false,
| default: 178,
| type: Number
| },
| },
| data() {
| return {
| uploadUrl: "/api/file/upload/no/expire",
| uploadHeaders: {
| 'Authentication': sessionStorage.getItem("token")
| },
| fileUrl: "",
| }
| },
| methods: {
| setReviewUrl(url) {
| this.fileUrl = url
| },
| getImgUploadIconStyle() {
| return {
| width: `${this.imgWidth}px`,
| height: `${this.imgHeight}px`,
| lineHeight: `${this.imgHeight}px`,
| };
| },
| getImgStyle() {
| return {
| width: `${this.imgWidth}px`,
| height: `${this.imgHeight}px`,
| };
| },
| handleUploadSuccess(res, file) {
| this.fileUrl = file.response.data.url
| this.$emit('getUploadUrl', file.response.data.url);
| },
| beforeUpload(file) {
| const limit = file.size / 1024 / 1024 < this.fileSizeLimitM;
|
| if (!limit) {
| this.$message.error(`上传文件大小不能超过 ${this.fileSizeLimitM}MB!`);
| }
| return limit;
| },
| }
| }
| </script>
|
| <style scoped>
| .avatar-uploader {
| text-align: center;
| width: 100%
| }
| .avatar-uploader-icon:hover {
| border-color: #409EFF;
| }
| .avatar-uploader-icon {
| font-size: 28px;
| color: #8c939d;
| text-align: center;
| border: 1px dashed #d9d9d9;
| border-radius: 6px;
| cursor: pointer;
|
| }
| .avatar {
| display: block;
| }
| </style>
|
|