From 9f8ac37497a502cb520422e5b91b6d288cc80d80 Mon Sep 17 00:00:00 2001 From: Pan Date: Tue, 12 Jun 2018 09:56:53 +0800 Subject: [PATCH] perf[UploadExcel-component]: set readerData to promise --- src/components/UploadExcel/index.vue | 36 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/components/UploadExcel/index.vue b/src/components/UploadExcel/index.vue index f17d8aed..9c1ec56a 100644 --- a/src/components/UploadExcel/index.vue +++ b/src/components/UploadExcel/index.vue @@ -3,7 +3,7 @@
Drop excel file here or - browse + browse
@@ -52,22 +52,28 @@ export default { const files = e.target.files const itemFile = files[0] // only use files[0] if (!itemFile) return - this.readerData(itemFile) - this.$refs['excel-upload-input'].value = null // fix can't select the same excel + this.loading = true + this.readerData(itemFile).then(() => { + this.$refs['excel-upload-input'].value = null // fix can't select the same excel + this.loading = false + }) }, readerData(itemFile) { - const reader = new FileReader() - reader.onload = e => { - const data = e.target.result - const fixedData = this.fixdata(data) - const workbook = XLSX.read(btoa(fixedData), { type: 'base64' }) - const firstSheetName = workbook.SheetNames[0] - const worksheet = workbook.Sheets[firstSheetName] - const header = this.get_header_row(worksheet) - const results = XLSX.utils.sheet_to_json(worksheet) - this.generateDate({ header, results }) - } - reader.readAsArrayBuffer(itemFile) + return new Promise((resolve, reject) => { + const reader = new FileReader() + reader.onload = e => { + const data = e.target.result + const fixedData = this.fixdata(data) + const workbook = XLSX.read(btoa(fixedData), { type: 'base64' }) + const firstSheetName = workbook.SheetNames[0] + const worksheet = workbook.Sheets[firstSheetName] + const header = this.get_header_row(worksheet) + const results = XLSX.utils.sheet_to_json(worksheet) + this.generateDate({ header, results }) + resolve() + } + reader.readAsArrayBuffer(itemFile) + }) }, fixdata(data) { let o = ''