Skip to content

Commit 1d0b26c

Browse files
committed
perf[upload-excel]: support drag upload
1 parent ea7e139 commit 1d0b26c

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/components/UploadExcel/index.vue

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<template>
22
<div>
3-
<el-button :loading="loading" type="primary" @click="handleUpload">select excel file</el-button>
43
<input id="excel-upload-input" type="file" accept=".xlsx, .xls" class="c-hide" @change="handkeFileChange">
4+
<div id="drop" @drop="handleDrop" @dragover="handleDragover" @dragenter="handleDragover">
5+
Drop excel file here or
6+
<el-button style="margin-left:16px;" size="mini" type="primary" @click="handleUpload">browse</el-button>
7+
</div>
58
</div>
69
</template>
710

@@ -22,16 +25,35 @@ export default {
2225
generateDate({ header, results }) {
2326
this.excelData.header = header
2427
this.excelData.results = results
25-
this.loading = false
2628
this.$emit('on-selected-file', this.excelData)
2729
},
30+
handleDrop(e) {
31+
e.stopPropagation()
32+
e.preventDefault()
33+
const files = e.dataTransfer.files
34+
if (files.length !== 1) {
35+
this.$message.error('Only support uploading one file!')
36+
return
37+
}
38+
const itemFile = files[0] // only use files[0]
39+
this.readerData(itemFile)
40+
e.stopPropagation()
41+
e.preventDefault()
42+
},
43+
handleDragover(e) {
44+
e.stopPropagation()
45+
e.preventDefault()
46+
e.dataTransfer.dropEffect = 'copy'
47+
},
2848
handleUpload() {
2949
document.getElementById('excel-upload-input').click()
3050
},
3151
handkeFileChange(e) {
32-
this.loading = true
3352
const files = e.target.files
3453
const itemFile = files[0] // only use files[0]
54+
this.readerData(itemFile)
55+
},
56+
readerData(itemFile) {
3557
const reader = new FileReader()
3658
reader.onload = e => {
3759
const data = e.target.result
@@ -75,4 +97,16 @@ export default {
7597
display: none;
7698
z-index: -9999;
7799
}
100+
#drop{
101+
border: 2px dashed #bbb;
102+
width: 600px;
103+
height: 160px;
104+
line-height: 160px;
105+
margin: 0 auto;
106+
font-size: 24px;
107+
border-radius: 5px;
108+
text-align: center;
109+
color: #bbb;
110+
position: relative;
111+
}
78112
</style>

0 commit comments

Comments
 (0)