From cbc3ddd8275d875e860e548dd326a98db1717e54 Mon Sep 17 00:00:00 2001 From: Pan Date: Wed, 20 Jun 2018 13:29:57 +0800 Subject: [PATCH] perf[uploadExcel]: add beforeUpload hock --- src/components/UploadExcel/index.vue | 34 ++++++++++++++++++++-------- src/views/excel/uploadExcel.vue | 14 +++++++++++- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/components/UploadExcel/index.vue b/src/components/UploadExcel/index.vue index 51a3704d..49658f48 100644 --- a/src/components/UploadExcel/index.vue +++ b/src/components/UploadExcel/index.vue @@ -12,6 +12,10 @@ import XLSX from 'xlsx' export default { + props: { + beforeUpload: Function, + onSuccess: Function + }, data() { return { loading: false, @@ -25,7 +29,7 @@ export default { generateDate({ header, results }) { this.excelData.header = header this.excelData.results = results - this.$emit('success', this.excelData) + this.onSuccess && this.onSuccess(this.excelData) }, handleDrop(e) { e.stopPropagation() @@ -36,8 +40,8 @@ export default { this.$message.error('Only support uploading one file!') return } - const itemFile = files[0] // only use files[0] - this.readerData(itemFile) + const rawFile = files[0] // only use files[0] + this.upload(rawFile) e.stopPropagation() e.preventDefault() }, @@ -51,13 +55,23 @@ export default { }, handleClick(e) { const files = e.target.files - const itemFile = files[0] // only use files[0] - if (!itemFile) return - this.readerData(itemFile).then(() => { - this.$refs['excel-upload-input'].value = null // fix can't select the same excel - }) + const rawFile = files[0] // only use files[0] + if (!rawFile) return + this.upload(rawFile) }, - readerData(itemFile) { + upload(rawFile) { + this.$refs['excel-upload-input'].value = null // fix can't select the same excel + + if (!this.beforeUpload) { + this.readerData(rawFile) + return + } + const before = this.beforeUpload(rawFile) + if (before) { + this.readerData(rawFile) + } + }, + readerData(rawFile) { this.loading = true return new Promise((resolve, reject) => { const reader = new FileReader() @@ -73,7 +87,7 @@ export default { this.loading = false resolve() } - reader.readAsArrayBuffer(itemFile) + reader.readAsArrayBuffer(rawFile) }) }, fixdata(data) { diff --git a/src/views/excel/uploadExcel.vue b/src/views/excel/uploadExcel.vue index 85439ef6..1d13e7cc 100644 --- a/src/views/excel/uploadExcel.vue +++ b/src/views/excel/uploadExcel.vue @@ -1,6 +1,6 @@