在 React (React.js) 中上传和读取 csv 文件的最简单方法是 react-papaparse (Home | Demo | Docs | Github)。
看下面的简单例子:
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 (
<>
<h5>Basic Upload</h5>
<CSVReader
ref={buttonRef}
onFileLoad={this.handleOnFileLoad}
onError={this.handleOnError}
noClick
noDrag
onRemoveFile={this.handleOnRemoveFile}
>
{({ file }) => (
<aside
style={{
display: 'flex',
flexDirection: 'row',
marginBottom: 10
}}
>
<button
type='button'
onClick={this.handleOpenDialog}
style={{
borderRadius: 0,
marginLeft: 0,
marginRight: 0,
width: '40%',
paddingLeft: 0,
paddingRight: 0
}}
>
Browe file
</button>
<div
style={{
borderWidth: 1,
borderStyle: 'solid',
borderColor: '#ccc',
height: 45,
lineHeight: 2.5,
marginTop: 5,
marginBottom: 5,
paddingLeft: 13,
paddingTop: 3,
width: '60%'
}}
>
{file && file.name}
</div>
<button
style={{
borderRadius: 0,
marginLeft: 0,
marginRight: 0,
paddingLeft: 20,
paddingRight: 20
}}
onClick={this.handleRemoveFile}
>
Remove
</button>
</aside>
)}
</CSVReader>
</>
)
}
}