I am learning reactjs
for building web application. Basically, my goal is I want to create reactjs
component that can upload local csv
file to the server (I created server js). To do so, I come across a basic reactjs
component implementation down below:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
var FormBox = React.createClass({
getInitialState: function () {
return {
file: '',
FilePreviewUrl: ''
};
},
pressButton: function () {
e.preventDefault();
console.log('handle uploading-', this.state.file);
},
uploadCSV: function (e) {
e.preventDefault();
let reader = new FileReader();
let file = e.target.files[0];
reader.onloadend = () => {
this.setState({
file: file,
FilePreviewUrl: reader.result
});
}
reader.readAsDataURL(file);
},
render: function () {
let {FilePreviewUrl} = this.state;
let FilePreview = null;
if (FilePreviewUrl) {
FilePreview = (<img src={FilePreviewUrl} />);
} else {
FilePreview = (<div className="previewText">Please select an Json for Preview</div>);
}
return (
<div>
<form action='.' enctype="multipart/form-data">
<input type='file' onChange={this.uploadCSV}/>
<button onClick={this.pressButton}> Get it </button>
</form>
<div className="filePreview">
{FilePreview}
</div>
</div>
)
}
})
}
ReactDOM.render(<FormBox />, document.getElementById('root'))
export default App;
but when I run my component I got an error which is not intuitive to me. Here is the error:
Failed to compile.
./src/App.js
Line 6: Parsing error: Unexpected token
4 |
5 | class App extends Component {
> 6 | var FormBox = React.createClass({
| ^
7 | getInitialState: function () {
8 | return {
9 | file: '',
How to fix this error? How to make above implementation works for the purpose of uploading csv file to the server? any idea?
clarification:
I am doing this because I intend to upload local csv file to the server and trigger API key that provided at back-end to populate the csv data to the database. I am newbie to reactjs so my above code may not be well shaped. can anyone help me how to make this works? any help would be appreciated.
goal:
Sine few smart people in SO
community suggested me that I was wrong about the implementation. What I am actually trying to do is to upload local files to server. How can I modify above code for creating reactjs
component for uploading file to server?