因?yàn)轫?xiàng)目采用element-ui,為了簡單直接使用它的 Upload組件來搭建讀取本地文檔功能,因?yàn)槭抢?code>js-xlsx在前端讀取,所以要把 auto-upload關(guān)掉
template
<el-upload
ref="upload"
action="/wm/upload/"
:show-file-list="false"
:on-change="readExcel"
:auto-upload="false">
<el-button
slot="trigger"
icon="el-icon-upload"
size="small"
type="primary">
上傳文件
</el-button>
</el-upload>
methods
readExcel(file) {
const fileReader = new FileReader();
fileReader.onload = (ev) => {
try {
const data = ev.target.result;
const workbook = XLSX.read(data, {
type: 'binary',
});
for (let sheet in workbook.Sheets) {
const sheetArray = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]);
}
} catch (e) {
this.$message.warning('文件類型不正確!');
return false;
}
};
fileReader.readAsBinaryString(file.raw);
},