简体   繁体   中英

Uploading file in ReactJs

I want to upload files from my local repository to given location. But error happens after clicking upload button. I got this error: Could not proxy request /api/user from localhost:3000 to http://localhost:3001/
Can you say where is the mistake?

class AddTeacher extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedFile: null
    }
  }
  onChangeHandler = event => {
    this.setState({selectedFile: event.target.files[0], loaded: 0})
  }
  onClickHandler = () => {
    const data = new FormData()
    data.append('file', this.state.selectedFile)
    axios.post("http://localhost:3001/api/officer/upload", data, { 
   })
   .then(res => { // then print response status
    alert(res.statusText)
 })
}

  render() {
    return (
      <div>
        <input type="file" name="file" onChange={this.onChangeHandler}/>
        <button
          type="button"
          class="btn btn-success btn-block"
          onClick={this.onClickHandler}>Upload</button>
      </div>
    );
  }
}
export default AddTeacher;

This error happen because http://localhost:3001/ is not working. I suspect your backend is not working. Try node server.js it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM