—— 阿尔伯特·爱因斯坦

程序员の奇妙冒险

ElementUIElementUI
2025-04-09 11:19阅读:199评论:0

ElementUI 上传图片拖拽排序

index.vue utils.js 使用 引入组件

  • index.vue
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<!--
*
-->
<template>
<div class="uploadWrapper">
<vuedraggable
class="vue-draggable"
:class="{ single: isSingle, maxHidden: isMaxHidden }"
v-model="imgList"
tag="ul"
draggable=".draggable-item"
@start="onDragStart"
@end="onDragEnd"
>
<!-- -->
<li
v-for="(item, index) in imgList"
:key="item + index"
class="draggable-item"
:style="{ width: width + 'px', height: height + 'px' }"
>
<el-image :src="item" :preview-src-list="[item]"></el-image>
<div class="shadow" @click="onRemoveHandler(index)">
<i class="el-icon-delete"></i>
</div>
<div class="showBottom" v-if="showBottom">
{{nameItams[item]}}
</div>
</li>
<!-- -->
<el-upload
slot="footer"
ref="uploadRef"
class="uploadBox"
:style="{ width: width + 'px', height: height + 'px' }"
:action="url"
:headers="headers"
accept=".JPG,.PNG,.JPEG,.GIF,.tif,.BMP"
:show-file-list="false"
:multiple="!isSingle"
:limit="limit"
:before-upload="beforeUpload"
:on-success="onSuccessUpload"
:on-exceed="onExceed"
>
<i class="el-icon-plus uploadIcon">
<span class="uploading" v-show="isUploading">...</span>
<span
v-if="!isUploading && limit && limit!==99 && !isSingle"
class="limitTxt"
>{{ limit }}</span>
</i>
</el-upload>
</vuedraggable>
</div>
</template>
 
<script>
import vuedraggable from 'vuedraggable' // 一款vue拖拽插件
import utils from './utils'
 
export default {
name: 'ImgUpload',
components: { vuedraggable },
props: {
// 上传地址
url: {
type: String,
// default: 'https://jsonplaceholder.typicode.com/posts/'
default: process.env.BASE_API + '/upload/uploadImage',
},
// 图片数据(图片url组成的数组) 通过v-model传递
value: {
type: Array,
default () {
return []
}
},
// 限制上传的图片数量
limit: {
type: Number,
default: 99
},
// 限制上传图片的文件大小(kb)
size: {
type: Number,
default: 500
},
// 是否是单图上传(单图上传就是已传图片和上传按钮重叠)
isSingle: {
type: Boolean,
default: false
},
// 是否使用图片压缩
useCompress: {
type: Boolean,
default: false
},
// 图片显示的宽度(px)
width: {
type: Number,
default: 100
},
// 图片显示的高度(px)
height: {
type: Number,
default: 100
},
// 底部是否显示图片原始名称
showBottom: {
type: Boolean,
default: true
}
},
 
data () {
return {
headers: {},
isUploading: false, // 正在上传状态
isFirstMount: true, // 控制防止重复回显
nameItams:{}
}
},
 
computed: {
// 图片数组数据
imgList: {
get () {
return this.value
},
set (val) {
if (val.length < this.imgList.length) {
// 判断是删除图片时同步el-upload数据
this.syncElUpload(val)
}
// 同步v-model
this.$emit('input', val)
}
},
// 控制达到最大限制时隐藏上传按钮
isMaxHidden () {
return this.imgList.length >= this.limit
}
},
 
watch: {
value: {
handler (val) {
if (this.isFirstMount && this.value.length > 0) {
this.syncElUpload()
}
},
deep: true
}
},
 
mounted () {
if (this.value.length > 0) {
this.syncElUpload()
}
},
 
methods: {
// 同步el-upload数据
syncElUpload (val) {
const imgList = val || this.imgList
this.$refs.uploadRef.uploadFiles = imgList.map((v, i) => {
return {
name: 'pic' + i,
url: v,
status: 'success',
uid: utils.createUniqueString()
}
})
this.isFirstMount = false
},
// 上传图片之前
beforeUpload (file) {
this.isFirstMount = false
if (utils.validImgUpload(file, this.size)) {
this.isUploading = true
return true
} else {
return false
}
},
// 上传完单张图片
onSuccessUpload (res, file, fileList) {
if (res.code === 200) {
console.log("上传文件-> ",file);
this.nameItams[res.data.url] = file.name;
if (this.imgList.length < this.limit) {
this.imgList.push(res.data.url)
}
} else {
this.syncElUpload()
this.$message({ type: 'error', message: file.name + '上传失败!' })
}
this.isUploading = false
},
// 移除单张图片
onRemoveHandler (index) {
this.$confirm('确定删除该图片?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(() => {
this.imgList = this.imgList.filter((v, i) => {
return i !== index
})
})
.catch(() => {})
},
// 超限
onExceed () {
this.$refs.uploadRef.abort() // 取消剩余接口请求
this.syncElUpload()
this.$message({
type: 'warning',
message: `${this.limit}`
})
},
onDragStart (e) {
e.target.classList.add('hideShadow')
},
onDragEnd (e) {
e.target.classList.remove('hideShadow')
}
},
}
</script>
 
<style rel="stylesheet/scss" lang="scss" scoped>
/deep/ .el-upload {
width: 100%;
height: 100%;
}
 
// 上传按钮
.uploadIcon {
width: 100%;
height: 100%;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 1px dashed #c0ccda;
background-color: #fbfdff;
border-radius: 6px;
font-size: 20px;
color: #999;
 
.limitTxt,
.uploading {
position: absolute;
bottom: 10%;
left: 0;
width: 100%;
font-size: 14px;
text-align: center;
}
}
 
// 拖拽
.vue-draggable {
display: flex;
flex-wrap: wrap;
 
.draggable-item {
margin-right: 5px;
margin-bottom: 5px;
border: 1px solid #ddd;
border-radius: 6px;
position: relative;
overflow: hidden;
 
.el-image {
width: 100%;
height: 100%;
}
.showBottom {
position: absolute;
bottom: 0;
height: 15px;
width: 100%;
line-height: 15px;
font-size: 5px;
color: #fff;
background-color: rgba(0,0,0,.5);
cursor: pointer;
padding: 0 5px 0 5px;
}
.shadow {
position: absolute;
top: 0;
right: 0;
background-color: rgba(0,0,0,.5);
opacity: 0;
transition: opacity .3s;
color: #fff;
font-size: 20px;
line-height: 20px;
padding: 2px;
cursor: pointer;
}
&:hover {
.shadow {
opacity: 1;
}
}
}
&.hideShadow {
.shadow {
display: none;
}
}
&.single {
overflow: hidden;
position: relative;
 
.draggable-item {
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
}
&.maxHidden {
.uploadBox {
display: none;
}
}
}
</style>
 
 
  • utils.js
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
/**
*
* @param {number} num Byte
* @param {number} digits
* @return {string} 2MB
*/
function toStorage (num, digits) {
digits = digits || 2
if (num < 1024) {
return num + 'B'
}
num = (num * 1000 / 1024)
const si = [
{ value: 1E18, symbol: 'E' },
{ value: 1E15, symbol: 'P' },
{ value: 1E12, symbol: 'T' },
{ value: 1E9, symbol: 'G' },
{ value: 1E6, symbol: 'M' },
{ value: 1E3, symbol: 'K' }
]
for (let i = 0; i < si.length; i++) {
if (num >= si[i].value) {
return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') +
si[i].symbol + 'B'
}
}
}
 
/**
*
* @param {file} file el-upload
* @param {number} size (kb) 10M
*/
function validImgUpload (file, size) {
size = +size || 10240
const isSizeOut = file.size / 1024 > size
if (isSizeOut) {
Message.error('上传图片大小不能超过' + toStorage(size * 1024))
}
return !isSizeOut
}
 
/**
*
* @return {string} ojgdvbvaua40
*/
function createUniqueString () {
const timestamp = +new Date() + ''
const randomNum = parseInt((1 + Math.random()) * 65536) + ''
return (+(randomNum + timestamp)).toString(32)
}
 
export default {
toStorage,
validImgUpload,
createUniqueString,
}
 
_PROTECTED0__
  • 引入组件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<template>
<div>
<DraggableImgUpload v-model="formData.imgList" :url="url"/>
</div>
</template>
 
 
import DraggableImgUpload from '@/components/DraggableImgUpload/index.vue'
export default {
components: { DraggableImgUpload },
}
 
 
评论(0)
暂无评论来抢沙发吧~