Javascript 如何确保上载Excel文件时,该文件显示在表格中

Javascript 如何确保上载Excel文件时,该文件显示在表格中,javascript,reactjs,axios,Javascript,Reactjs,Axios,我想确保在上传xlsx文件时。 数据显示在我创建的表中。 对于上传,我使用Axios软件包。 我还没有后端,我现在通过前端做所有事情。 我想知道我怎样才能完成它 这是表格的代码: import React from 'react'; import './ProductImportTable.css'; import classnames from 'classnames'; import NourishmentGroup from "./NourishmentGroup/Nourish

我想确保在上传xlsx文件时。 数据显示在我创建的表中。 对于上传,我使用Axios软件包。 我还没有后端,我现在通过前端做所有事情。 我想知道我怎样才能完成它

这是表格的代码:

import React from 'react';
import './ProductImportTable.css';
import classnames from 'classnames';
import NourishmentGroup from "./NourishmentGroup/NourishmentGroup";
import NourishmentType from "./NourishmentType/NourishmentType";
import {ConnectForm} from "../../Pages/ProcurementAnalysis/ProcurementAnalysis";
export default function ProductImportTable() {
    const renderProductImportTable = (register, errors, watch) => {
        return (
            <div className="productImportTableContainer">
                <table className="productImportTable">
                    <thead>
                    <tr>
                        <th><p>Omschrijving product</p></th>
                        <th><p>Soort product</p></th>
                        <th><p>Productgroep</p></th>
                        <th><p>Inkoopvolume in €</p></th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>
                            <div className={
                                classnames('field', {incorrect: errors.description})
                            }>
                            <input type="text" name="description" ref={register({required: true})}/>
                            </div>
                    {errors.description && <span className={"error"}>This field is required.</span>}
                        </td>
                        <td>
                            <div className={
                                classnames('field', {incorrect: errors.nourishmentType})
                            }>
                                <NourishmentType/>
                            </div>
                            {errors.nourishmentType &&
                            <span className={"error"}>This field is required.</span>}
                        </td>
                        <td>
                            <div className={
                                classnames('field', {incorrect: errors.nourishmentGroup})
                            }>
                                <NourishmentGroup/>
                            </div>
                            {errors.nourishmentGroup &&
                            <span className={"error"}>This field is required.</span>}
                        </td>
                        <td>
                            <div className={
                                classnames('field', {incorrect: errors.procurementVolumeInEuros})
                            }>
               <input name="procurementVolumeInEuros" ref={register({required: true})} type="number"
                                       min="0"
                                       step="0.01"/>
                            </div>
       {errors.procurementVolumeInEuros && <span className={"error"}>This field is required.</span>}
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        );
    }

    return (
        <ConnectForm>
            {({register, errors, watch}) => renderProductImportTable(register, errors, watch)}
        </ConnectForm>
    );
}
从“React”导入React;
导入“/productImportable.css”;
从“类名称”导入类名称;
从“/滋养组/滋养组”导入滋养组;
从“/滋养类型/滋养类型”导入滋养类型;
从“./../Pages/ProcurementAnalysis/ProcurementAnalysis”导入{ConnectForm}”;
导出默认函数ProductImportTable(){
const RenderProductImportable=(寄存器、错误、监视)=>{
返回(
Omschrijving产品

索特产品

产品组

Inkoop体积(单位:欧元)

{errors.description&&此字段是必需的。} {errors.nurtmenttype&& 此字段是必需的。} {errors.nurtmentgroup&& 此字段是必需的。} {errors.procurementVolumeNeuros&&此字段为必填项。} ); } 返回( {({register,errors,watch})=>renderProductImportable(register,errors,watch)} ); }

这是上载页面的代码:

import React, { Component } from "react";
import axios from "axios";
import "./Upload.css";

class Uploadscherm extends Component {
  state = {
    // Initially, no file is selected
    selectedFile: null,
  };

  // On file select (from the pop up)
  onFileChange = (event) => {
    // Update the state
    this.setState({ selectedFile: event.target.files[0] });
  };

  // On file upload (click the upload button)
  onFileUpload = () => {
    // Create an object of formData
    const formData = new FormData();

    // Update the formData object
    formData.append(
      "myFile",
      this.state.selectedFile,
      this.state.selectedFile.name
    );

    // Details of the uploaded file
    console.log(this.state.selectedFile);

    // Request made to the backend api
    // Send formData object
    axios.post("api/uploadfile", formData);
  };

  // File content to be displayed after
  // file upload is complete
  fileData = () => {
    if (this.state.selectedFile) {
      return (
        <div>
          <p className="OmB">File Name: {this.state.selectedFile.name}</p>
          {/* <p className="OmB">File Type: {this.state.selectedFile.type}</p> */}
          <p className="OmB">
            Last Modified:{" "}
            {this.state.selectedFile.lastModifiedDate.toDateString()}
          </p>
        </div>
      );
    } else {
      return (
        <div>
          <br />
        </div>
      );
    }
  };

  render() {
    return (
      <div>
        <h1 className="Title">Greendish</h1>
        <h3 className="Omschrijving">Upload uw Inkoopcheck bestand!</h3>
        <div className="Uploaden">
          <input type="file" onChange={this.onFileChange} />
          <button onClick={this.onFileUpload}>Upload!</button>
        </div>
        {this.fileData()}
      </div>
    );
  }
}

export default Uploadscherm;
import React,{Component}来自“React”;
从“axios”导入axios;
导入“/Upload.css”;
类Uploadscherm扩展组件{
状态={
//最初,未选择任何文件
selectedFile:null,
};
//在文件上选择(从弹出窗口)
onFileChange=(事件)=>{
//更新状态
this.setState({selectedFile:event.target.files[0]});
};
//文件上载时(单击上载按钮)
onFileUpload=()=>{
//创建formData的对象
const formData=new formData();
//更新formData对象
formData.append(
“我的文件”,
this.state.selectedFile,
this.state.selectedFile.name
);
//上载文件的详细信息
console.log(this.state.selectedFile);
//向后端api发出的请求
//发送表单数据对象
post(“api/uploadfile”,formData);
};
//之后要显示的文件内容
//文件上传完成
文件数据=()=>{
if(this.state.selectedFile){
返回(
文件名:{this.state.selectedFile.Name}

{/*

文件类型:{this.state.selectedFile.Type}

*/}

上次修改:{“”} {this.state.selectedFile.lastModifiedDate.toDateString()}

); }否则{ 返回(
); } }; render(){ 返回( 菜鸟 上传uw Inkoopcheck bestand! 上传! {this.fileData()} ); } } 导出默认的Uploadscherm;