[英]Show an error to upload the file or disable de button
I have this React code, also you can check the code here https://stackblitz.com/edit/react-excel-to-json-parser .我有这个 React 代码,你也可以在这里查看代码https://stackblitz.com/edit/react-excel-to-json-parser 。 When I click the button 'Process Triggers' without upload a file.
当我单击“处理触发器”按钮而不上传文件时。 The app breaks how can handle an alert error the user must upload the file or disable the button 'Process Triggers' until the user upload a file.
该应用程序打破了如何处理用户必须上传文件或禁用“处理触发器”按钮直到用户上传文件的警报错误。
import React, { Component } from 'react';
import { Fabric } from 'office-ui-fabric-react/lib/Fabric';
import { DefaultButton } from 'office-ui-fabric-react/lib/Button';
import XLSX from 'xlsx';
import { make_cols } from './MakeColumns';
import { SheetJSFT } from './types';
class ExcelReader extends Component {
constructor(props) {
super(props);
this.state = {
file: {},
data: [],
cols: []
}
this.handleFile = this.handleFile.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
const files = e.target.files;
if (files && files[0]) this.setState({ file: files[0] });
};
handleFile() {
/* 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);
};
}
render() {
return (
<div>
<label htmlFor="file">Upload an excel to Process Triggers</label>
<br />
<input type="file" className="form-control" id="file" accept={SheetJSFT} onChange={this.handleChange} />
<br />
<input type='submit'
value="Process Triggers"
onClick={this.handleFile} />
</div>
)
}
}
export default ExcelReader;
``
Put handleFile() code inside a try catch block将 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);
}
}
You can add the disabled
attribute to your submit button, based on a local state.您可以根据本地 state 将
disabled
属性添加到提交按钮。
Your new local state would be:您的新本地 state 将是:
this.state = {
file: {},
isFileLoaded: false,
data: [],
cols: []
}
then update that state on your handleChange
:然后更新您的 handleChange 上的
handleChange
:
handleChange(e) {
const files = e.target.files;
if (files && files[0]) this.setState({ file: files[0], isFileLoaded: true });
};
And finally your submit input:最后你的提交输入:
<input type='submit' disabled={!this.state.isFileLoaded} value="Process Triggers" onClick={this.handleFile} />
Here is a working demo: https://stackblitz.com/edit/react-excel-to-json-parser-g49uhh这是一个工作演示: https://stackblitz.com/edit/react-excel-to-json-parser-g49uhh
Do you want to always show the error ( when didn't upload a file )?您是否要始终显示错误(未上传文件时)? For disabling the submit button, you can try with
要禁用提交按钮,您可以尝试使用
<input type='submit'
disabled={!this.state.file}
value="Process Triggers"
onClick={this.handleFile}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.