12
12
import XLSX from ' xlsx'
13
13
14
14
export default {
15
+ props: {
16
+ beforeUpload: Function ,
17
+ onSuccess: Function
18
+ },
15
19
data () {
16
20
return {
17
21
loading: false ,
@@ -25,7 +29,7 @@ export default {
25
29
generateDate ({ header, results }) {
26
30
this .excelData .header = header
27
31
this .excelData .results = results
28
- this .$emit ( ' success ' , this .excelData )
32
+ this .onSuccess && this . onSuccess ( this .excelData )
29
33
},
30
34
handleDrop (e ) {
31
35
e .stopPropagation ()
@@ -36,8 +40,8 @@ export default {
36
40
this .$message .error (' Only support uploading one file!' )
37
41
return
38
42
}
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 )
41
45
e .stopPropagation ()
42
46
e .preventDefault ()
43
47
},
@@ -51,13 +55,23 @@ export default {
51
55
},
52
56
handleClick (e ) {
53
57
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
+ }
59
73
},
60
- readerData (itemFile ) {
74
+ readerData (rawFile ) {
61
75
this .loading = true
62
76
return new Promise ((resolve , reject ) => {
63
77
const reader = new FileReader ()
@@ -73,7 +87,7 @@ export default {
73
87
this .loading = false
74
88
resolve ()
75
89
}
76
- reader .readAsArrayBuffer (itemFile )
90
+ reader .readAsArrayBuffer (rawFile )
77
91
})
78
92
},
79
93
fixdata (data ) {
0 commit comments