How to upload and read CSV files in React.js?

后端 未结 3 1320
终归单人心
终归单人心 2020-12-23 12:16

I would like the user to upload a .csv file, and then have the browser be able to parse the data from that file. I am using ReactJS. How would this work? Thanks.

3条回答
  •  春和景丽
    2020-12-23 12:31

    The easiest way to upload and read csv files in React (React.js) is react-papaparse ( Home | Demo | Docs | Github ).

    Look at the simply example below:

    import React, { Component } from 'react'
    import { CSVReader } from 'react-papaparse'
    
    const buttonRef = React.createRef()
    
    export default class CSVReader1 extends Component {
      handleOpenDialog = (e) => {
        // Note that the ref is set async, so it might be null at some point
        if (buttonRef.current) {
          buttonRef.current.open(e)
        }
      }
    
      handleOnFileLoad = (data) => {
        console.log('---------------------------')
        console.log(data)
        console.log('---------------------------')
      }
    
      handleOnError = (err, file, inputElem, reason) => {
        console.log(err)
      }
    
      handleOnRemoveFile = (data) => {
        console.log('---------------------------')
        console.log(data)
        console.log('---------------------------')
      }
    
      handleRemoveFile = (e) => {
        // Note that the ref is set async, so it might be null at some point
        if (buttonRef.current) {
          buttonRef.current.removeFile(e)
        }
      }
    
      render() {
        return (
          <>
            
    Basic Upload
    {({ file }) => ( )} ) } }

提交回复
热议问题