Skip to content

Commit cbc3ddd

Browse files
committed
perf[uploadExcel]: add beforeUpload hock
1 parent 9cf00fd commit cbc3ddd

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/components/UploadExcel/index.vue

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import XLSX from 'xlsx'
1313
1414
export default {
15+
props: {
16+
beforeUpload: Function,
17+
onSuccess: Function
18+
},
1519
data() {
1620
return {
1721
loading: false,
@@ -25,7 +29,7 @@ export default {
2529
generateDate({ header, results }) {
2630
this.excelData.header = header
2731
this.excelData.results = results
28-
this.$emit('success', this.excelData)
32+
this.onSuccess && this.onSuccess(this.excelData)
2933
},
3034
handleDrop(e) {
3135
e.stopPropagation()
@@ -36,8 +40,8 @@ export default {
3640
this.$message.error('Only support uploading one file!')
3741
return
3842
}
39-
const itemFile = files[0] // only use files[0]
40-
this.readerData(itemFile)
43+
const rawFile = files[0] // only use files[0]
44+
this.upload(rawFile)
4145
e.stopPropagation()
4246
e.preventDefault()
4347
},
@@ -51,13 +55,23 @@ export default {
5155
},
5256
handleClick(e) {
5357
const files = e.target.files
54-
const itemFile = files[0] // only use files[0]
55-
if (!itemFile) return
56-
this.readerData(itemFile).then(() => {
57-
this.$refs['excel-upload-input'].value = null // fix can't select the same excel
58-
})
58+
const rawFile = files[0] // only use files[0]
59+
if (!rawFile) return
60+
this.upload(rawFile)
61+
},
62+
upload(rawFile) {
63+
this.$refs['excel-upload-input'].value = null // fix can't select the same excel
64+
65+
if (!this.beforeUpload) {
66+
this.readerData(rawFile)
67+
return
68+
}
69+
const before = this.beforeUpload(rawFile)
70+
if (before) {
71+
this.readerData(rawFile)
72+
}
5973
},
60-
readerData(itemFile) {
74+
readerData(rawFile) {
6175
this.loading = true
6276
return new Promise((resolve, reject) => {
6377
const reader = new FileReader()
@@ -73,7 +87,7 @@ export default {
7387
this.loading = false
7488
resolve()
7589
}
76-
reader.readAsArrayBuffer(itemFile)
90+
reader.readAsArrayBuffer(rawFile)
7791
})
7892
},
7993
fixdata(data) {

src/views/excel/uploadExcel.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="app-container">
3-
<upload-excel-component @success='handleSuccess'></upload-excel-component>
3+
<upload-excel-component :on-success='handleSuccess' :before-upload="beforeUpload"></upload-excel-component>
44
<el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
55
<el-table-column v-for='item of tableHeader' :prop="item" :label="item" :key='item'>
66
</el-table-column>
@@ -21,6 +21,18 @@ export default {
2121
}
2222
},
2323
methods: {
24+
beforeUpload(file) {
25+
const isLt2M = file.size / 1024 / 1024 < 1
26+
27+
if (isLt2M) {
28+
return true
29+
}
30+
this.$message({
31+
message: 'Please do not upload files larger than 2m in size.',
32+
type: 'warning'
33+
})
34+
return false
35+
},
2436
handleSuccess({ results, header }) {
2537
this.tableData = results
2638
this.tableHeader = header

0 commit comments

Comments
 (0)