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
| <template>
| <div class="my-upload">
| <el-upload :file-list="fileList" class="upload-demo" action="/sccg/file/medias" :multiple="multiple"
| :show-file-list="flag" :before-upload="beforeUpload" :limit="limit" :on-success="handleSuccess"
| :on-error="handleError" :list-type="listType" :headers="getToken()">
| <div class="upload-btn" :style="{'height':btnHeight,'width':btnWidth}">
| <i class="el-icon-plus"></i>
| <span>上传图片</span>
| </div>
| </el-upload>
| </div>
| </template>
| <script>
|
| export default {
| data() {
| return {
| // 文件列表
| fileList: [],
| // 是否显示文件列表
| flag: true,
| // 多选
| multiple: false,
| // 限制
| limit: 50,
| // 按钮高度
| btnHeight: '120px',
| // 按钮宽度
| btnWidth: '120px',
| // 文件列表类型
| listType: 'picture-card'
| };
| },
| methods: {
| // 上传之前回调
| beforeUpload(rawFile) {
| console.log(rawFile);
| if (rawFile.type !== 'image/png' && rawFile.type !== 'image/svg+xml' && rawFile.type !== 'image/jpg' && rawFile.type !== 'image/jpeg') {
| this.$message.error('图片必须是 jpg/svg/jpeg/png 格式!')
| return false
| } else if (rawFile.size / 105 / 105 > 2) {
| this.$message.error('上传图片不能超过 2MB!')
| return false
| }
| return true
| },
| // 上传成功回调
| handleSuccess(res, file, filelist) {
| console.log(res);
| },
| // 上传失败回调
| handleError(err, file, fileList) {
| console.log(err);
| },
| // 获取token
| getToken() {
| const token = sessionStorage.getItem('token');
| const tokenHead = sessionStorage.getItem('tokenHead');
| if (token && tokenHead) {
| return { Authorization: tokenHead + token }
| }
| }
| },
| props: {
|
| }
| }
| </script>
| <style lang="scss" scoped>
| .my-upload {
| .upload-demo {
| display: flex;
| }
|
| :deep(.el-upload--picture-card) {
| width: 120px;
| height: 120px;
| }
| :deep(.el-upload-list__item){
| width: 120px;
| height: 120px;
| }
| .upload-btn {
| // background-color: rgba(249, 249, 249, 1);
| display: flex;
| flex-direction: column;
| justify-content: center;
| align-items: center;
| border-radius: 4px;
|
| &:hover i{
| color: #409eff;
| }
|
| i {
| font-size: 30px;
| font-weight: 650;
| }
|
| span {
| line-height: 22px;
| }
| }
| }
| </style>
|
|