forked from AkkomaGang/admin-fe
perf[uploadExcel]: add beforeUpload hock
This commit is contained in:
parent
9cf00fd63a
commit
cbc3ddd827
2 changed files with 37 additions and 11 deletions
|
@ -12,6 +12,10 @@
|
||||||
import XLSX from 'xlsx'
|
import XLSX from 'xlsx'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
props: {
|
||||||
|
beforeUpload: Function,
|
||||||
|
onSuccess: Function
|
||||||
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
loading: false,
|
loading: false,
|
||||||
|
@ -25,7 +29,7 @@ export default {
|
||||||
generateDate({ header, results }) {
|
generateDate({ header, results }) {
|
||||||
this.excelData.header = header
|
this.excelData.header = header
|
||||||
this.excelData.results = results
|
this.excelData.results = results
|
||||||
this.$emit('success', this.excelData)
|
this.onSuccess && this.onSuccess(this.excelData)
|
||||||
},
|
},
|
||||||
handleDrop(e) {
|
handleDrop(e) {
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
|
@ -36,8 +40,8 @@ export default {
|
||||||
this.$message.error('Only support uploading one file!')
|
this.$message.error('Only support uploading one file!')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const itemFile = files[0] // only use files[0]
|
const rawFile = files[0] // only use files[0]
|
||||||
this.readerData(itemFile)
|
this.upload(rawFile)
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
},
|
},
|
||||||
|
@ -51,13 +55,23 @@ export default {
|
||||||
},
|
},
|
||||||
handleClick(e) {
|
handleClick(e) {
|
||||||
const files = e.target.files
|
const files = e.target.files
|
||||||
const itemFile = files[0] // only use files[0]
|
const rawFile = files[0] // only use files[0]
|
||||||
if (!itemFile) return
|
if (!rawFile) return
|
||||||
this.readerData(itemFile).then(() => {
|
this.upload(rawFile)
|
||||||
this.$refs['excel-upload-input'].value = null // fix can't select the same excel
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
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
|
this.loading = true
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const reader = new FileReader()
|
const reader = new FileReader()
|
||||||
|
@ -73,7 +87,7 @@ export default {
|
||||||
this.loading = false
|
this.loading = false
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
reader.readAsArrayBuffer(itemFile)
|
reader.readAsArrayBuffer(rawFile)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
fixdata(data) {
|
fixdata(data) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="app-container">
|
<div class="app-container">
|
||||||
<upload-excel-component @success='handleSuccess'></upload-excel-component>
|
<upload-excel-component :on-success='handleSuccess' :before-upload="beforeUpload"></upload-excel-component>
|
||||||
<el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
|
<el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
|
||||||
<el-table-column v-for='item of tableHeader' :prop="item" :label="item" :key='item'>
|
<el-table-column v-for='item of tableHeader' :prop="item" :label="item" :key='item'>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
|
@ -21,6 +21,18 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
beforeUpload(file) {
|
||||||
|
const isLt2M = file.size / 1024 / 1024 < 1
|
||||||
|
|
||||||
|
if (isLt2M) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
this.$message({
|
||||||
|
message: 'Please do not upload files larger than 2m in size.',
|
||||||
|
type: 'warning'
|
||||||
|
})
|
||||||
|
return false
|
||||||
|
},
|
||||||
handleSuccess({ results, header }) {
|
handleSuccess({ results, header }) {
|
||||||
this.tableData = results
|
this.tableData = results
|
||||||
this.tableHeader = header
|
this.tableHeader = header
|
||||||
|
|
Loading…
Reference in a new issue