3 回答

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
將 handleFile() 代碼放在 try catch 塊中
handleFile() {
try {
/* Boilerplate to set up FileReader */
const reader = new FileReader();
const rABS = !!reader.readAsBinaryString;
reader.onload = (e) => {
/* Parse data */
const bstr = e.target.result;
const wb = XLSX.read(bstr, { type: rABS ? 'binary' : 'array', bookVBA : true });
/* Get first worksheet */
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
/* Convert array of arrays */
const data = XLSX.utils.sheet_to_json(ws);
/* Update state */
this.setState({ data: data, cols: make_cols(ws['!ref']) }, () => {
console.log(JSON.stringify(this.state.data, null, 2));
});
};
if (rABS) {
reader.readAsBinaryString(this.state.file);
} else {
reader.readAsArrayBuffer(this.state.file);
};
} catch(err) {
console.log(err);
}
}

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以disabled根據(jù)本地狀態(tài)將屬性添加到提交按鈕。
您的新本地狀態(tài)將是:
this.state = {
file: {},
isFileLoaded: false,
data: [],
cols: []
}
然后更新你的狀態(tài)handleChange:
handleChange(e) {
const files = e.target.files;
if (files && files[0]) this.setState({ file: files[0], isFileLoaded: true });
};
最后是您的提交輸入:
<input type='submit' disabled={!this.state.isFileLoaded} value="Process Triggers" onClick={this.handleFile} />
這是一個(gè)工作演示:https ://stackblitz.com/edit/react-excel-to-json-parser-g49uhh

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
您是否要始終顯示錯(cuò)誤(未上傳文件時(shí))?要禁用提交按鈕,您可以嘗試
<input type='submit'
disabled={!this.state.file}
value="Process Triggers"
onClick={this.handleFile}
/>
添加回答
舉報(bào)