Skip to content

Commit 088c96b

Browse files
author
weilei
committed
feat(examples): 增加自定义拖拽上传区域和导航按钮功能,支持文件上传和删除操作
1 parent f8eab3c commit 088c96b

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

examples/src/views/Attachments.vue

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,62 @@
9797
<el-button type="danger" @click="resetCustomFiles">清空自定义文件</el-button>
9898
</div>
9999
</div>
100+
101+
<div class="demo-block">
102+
<h3>自定义拖拽区域</h3>
103+
<div style="display: flex; flex-direction: column; gap: 12px;">
104+
<p>
105+
设置全屏拖拽上传:
106+
<el-switch v-model="isFull" />
107+
</p>
108+
<el-x-attachments
109+
:file-list="dragFiles"
110+
:http-request="handleDragHttpRequest"
111+
:items="dragFiles"
112+
drag
113+
:drag-target="dragArea"
114+
:before-upload="handleBeforUpload"
115+
:hide-upload="false"
116+
@upload-drop="handleDragUploadDrop"
117+
@delete-card="handleDragDeleteCard"
118+
/>
119+
120+
<div id="drag-area" style="border: 2px dashed #ccc; padding: 20px; height: 250px; text-align: center; display: flex; align-items: center; justify-content: center;">在此处拖拽文件上传</div>
121+
</div>
122+
<div style="margin-top: 10px;">
123+
<el-button type="danger" @click="resetDragFiles">清空文件</el-button>
124+
</div>
125+
</div>
126+
127+
<div class="demo-block">
128+
<h3>自定义导航按钮</h3>
129+
<div style="display: flex; flex-direction: column; gap: 12px;">
130+
<div style="position: relative;">
131+
<el-x-attachments
132+
:file-list="navFiles"
133+
:http-request="handleNavHttpRequest"
134+
:items="navFiles"
135+
drag
136+
overflow="scrollX"
137+
:before-upload="handleBeforUpload"
138+
:hide-upload="false"
139+
@upload-drop="handleNavUploadDrop"
140+
@delete-card="handleNavDeleteCard"
141+
>
142+
<template slot="prev-button" slot-scope="{ show, onScrollLeft }">
143+
<button v-if="show" class="custom-prev" @click="onScrollLeft">👈</button>
144+
</template>
145+
<template slot="next-button" slot-scope="{ show, onScrollRight }">
146+
<button v-if="show" class="custom-next" @click="onScrollRight">👉</button>
147+
</template>
148+
</el-x-attachments>
149+
</div>
150+
</div>
151+
<div style="margin-top: 10px;">
152+
<el-button type="primary" @click="generateNavFiles">生成演示文件</el-button>
153+
<el-button type="danger" @click="resetNavFiles">清空文件</el-button>
154+
</div>
155+
</div>
100156
</el-card>
101157
</div>
102158
</template>
@@ -109,6 +165,10 @@ export default {
109165
files: [],
110166
demoFiles: [],
111167
customFiles: [],
168+
dragFiles: [],
169+
navFiles: [],
170+
isFull: false,
171+
dragArea: 'drag-area',
112172
limit: 5,
113173
hideUpload: false,
114174
drag: true,
@@ -134,6 +194,21 @@ export default {
134194
},
135195
mounted() {
136196
this.generateDemoFiles()
197+
this.generateNavFiles()
198+
},
199+
watch: {
200+
isFull: {
201+
handler(newVal) {
202+
console.log('isFull', newVal)
203+
if (newVal) {
204+
this.dragArea = document.body
205+
} else {
206+
this.dragArea = 'drag-area'
207+
}
208+
},
209+
immediate: true,
210+
deep: true,
211+
},
137212
},
138213
methods: {
139214
handleBeforUpload(file) {
@@ -326,6 +401,120 @@ export default {
326401
327402
this.$message.success('已生成5个自定义演示文件')
328403
},
404+
async handleDragUploadDrop(files, props) {
405+
if (files && files.length > 0) {
406+
if (files[0].type === '') {
407+
this.$message.error('禁止上传文件夹!')
408+
return false
409+
}
410+
411+
for (let index = 0; index < files.length; index++) {
412+
const file = files[index]
413+
await this.handleDragHttpRequest({ file })
414+
}
415+
}
416+
},
417+
async handleDragHttpRequest(options) {
418+
this.$message.info('上传中...')
419+
420+
return new Promise((resolve) => {
421+
setTimeout(() => {
422+
const res = {
423+
message: '文件上传成功',
424+
fileName: options.file.name,
425+
uid: options.file.uid,
426+
fileSize: options.file.size,
427+
imgFile: options.file,
428+
}
429+
this.dragFiles.push({
430+
id: this.dragFiles.length,
431+
uid: res.uid,
432+
name: res.fileName,
433+
fileSize: res.fileSize,
434+
imgFile: res.imgFile,
435+
showDelIcon: true,
436+
imgVariant: 'square',
437+
})
438+
this.$message.success('上传成功')
439+
resolve(res)
440+
}, 1000)
441+
})
442+
},
443+
handleDragDeleteCard(item, index) {
444+
console.log('删除拖拽文件', item, index)
445+
this.dragFiles = this.dragFiles.filter((items) => items.id !== item.id)
446+
this.$message.success('删除成功')
447+
},
448+
resetDragFiles() {
449+
this.dragFiles = []
450+
},
451+
async handleNavUploadDrop(files, props) {
452+
if (files && files.length > 0) {
453+
if (files[0].type === '') {
454+
this.$message.error('禁止上传文件夹!')
455+
return false
456+
}
457+
458+
for (let index = 0; index < files.length; index++) {
459+
const file = files[index]
460+
await this.handleNavHttpRequest({ file })
461+
}
462+
}
463+
},
464+
async handleNavHttpRequest(options) {
465+
this.$message.info('上传中...')
466+
467+
return new Promise((resolve) => {
468+
setTimeout(() => {
469+
const res = {
470+
message: '文件上传成功',
471+
fileName: options.file.name,
472+
uid: options.file.uid,
473+
fileSize: options.file.size,
474+
imgFile: options.file,
475+
}
476+
this.navFiles.push({
477+
id: this.navFiles.length,
478+
uid: res.uid,
479+
name: res.fileName,
480+
fileSize: res.fileSize,
481+
imgFile: res.imgFile,
482+
showDelIcon: true,
483+
imgVariant: 'square',
484+
})
485+
this.$message.success('上传成功')
486+
resolve(res)
487+
}, 1000)
488+
})
489+
},
490+
handleNavDeleteCard(item, index) {
491+
console.log('删除导航文件', item, index)
492+
this.navFiles = this.navFiles.filter((items) => items.id !== item.id)
493+
this.$message.success('删除成功')
494+
},
495+
resetNavFiles() {
496+
this.navFiles = []
497+
},
498+
generateNavFiles() {
499+
const typeList = Object.keys(this.colorMap)
500+
this.navFiles = []
501+
502+
for (let index = 0; index < 15; index++) {
503+
this.navFiles.push({
504+
id: index,
505+
uid: index,
506+
name: `导航文件${index}`,
507+
fileSize: 1024 * (index + 1),
508+
fileType: typeList[Math.floor(Math.random() * typeList.length)],
509+
url: 'https://www.baidu.com',
510+
thumbUrl: 'https://www.baidu.com',
511+
imgFile: new File([], 'test.txt'),
512+
showDelIcon: true,
513+
})
514+
}
515+
516+
this.$message.success('已生成15个导航演示文件')
517+
},
329518
},
330519
}
331520
</script>
@@ -387,4 +576,34 @@ h4 {
387576
color: #909399;
388577
font-size: 12px;
389578
}
579+
580+
.custom-prev,
581+
.custom-next {
582+
position: absolute;
583+
top: 50%;
584+
transform: translateY(-50%);
585+
z-index: 10;
586+
background-color: rgba(0, 0, 0, 0.5);
587+
color: white;
588+
border: 2px solid rgba(255, 255, 255, 0.5);
589+
padding: 8px 16px;
590+
border-radius: 4px;
591+
font-size: 14px;
592+
transition: all 0.3s ease;
593+
}
594+
595+
.custom-prev {
596+
left: 8px;
597+
}
598+
599+
.custom-next {
600+
right: 8px;
601+
}
602+
603+
.custom-prev:hover,
604+
.custom-next:hover {
605+
background-color: rgba(0, 0, 0, 0.8);
606+
color: white;
607+
border-color: rgba(255, 255, 255, 0.8);
608+
}
390609
</style>

0 commit comments

Comments
 (0)