javascript - 显示错误以上传文件或禁用按钮

标签 javascript reactjs

我有这个 React 代码,你也可以在这里查看代码 https://stackblitz.com/edit/react-excel-to-json-parser .当我在没有上传文件的情况下单击“处理触发器”按钮时。该应用程序打破了如何处理用户必须上传文件或禁用按钮“处理触发器”直到用户上传文件的警报错误。

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;
``

最佳答案

将 handleFile() 代码放入 try catch block 中

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);
    }
  }

关于javascript - 显示错误以上传文件或禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63126885/

相关文章:

javascript - 从数组中删除元素时 id 未定义

javascript - 重定向到不重定向到所需的 url - React

ios - 找不到 -lAppAuth 的库

reactjs - GatsbyJS 网站非页面组件中的 GraphQL 查询投诉

css - React Router 似乎会阻止自定义 CSS

javascript - React 子图表组件未更新

javascript - AngularJS自定义指令如何访问对象值?

javascript - 在 map 中心显示信息窗口 - Google 避开高速公路

javascript - React-Redux API 文档符号

javascript - 构建 SPA 应用程序的原因\和好处