1

I am trying to submit a file and text input to backend with POST request, the code looks like this,

state={
   text:'',
   file:''
}

handleTextChange = (e) => {
   this.setState({[e.target.name]:e.target.value})
}

handleFileChange = (e) => {
   this.setState({file:e.target.files[0]})
}

handleSubmit = (e) => {
   const {text,file} = this.state

   //POST request with text and file
}

But the file is always empty, If I console.log(e.target.files[0]), it shows a FileList in console but somehow it is not updated in state I am quite new in react, your help is very much appreciated, thanks



EDIT: the form looks like this

<form action="" className="w-100" onSubmit={(e) => this.handleSubmit(e)}>
   <div className="pt-1 pb-3">
    <TextAreaAutosize
         className="form-control"
         placeholder="write some text here"
         minRows="4"
         name="text"
         onChange={(e) => this.handleChange(e)}
     />
   </div>
   <div className="w-100 d-flex justify-content-between align-items-center">
      <div>
          <input
               type="file"
               className="form-control-file"
               name="file"
               onChange={(e) => this.handleFileChange(e)}
          />
       </div>
       <div>
           <button type="submit" className="btn btn-primary">
              Post
           </button>
       </div>
     </div>
 </form>
2
  • 1
    plaese attach full component ,HTML and 3rd partys Commented Jun 17, 2020 at 10:01
  • 2
    React, {component} and TextAreaAutoSize are the only packages used and form is simple 'textarea' and 'file input' in a bootstrap modal Commented Jun 17, 2020 at 10:04

1 Answer 1

1

sandbox:

https://codesandbox.io/s/friendly-banzai-8ifw4?fontsize=14&hidenavigation=1&theme=dark

try this one works just fine

before install

run npm install @material-ui/core

import React, { Component } from 'react';
import './App.css';
import { TextareaAutosize } from '@material-ui/core';



class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      text: '',
      file: ''
    }
    this.handleFileChange=this.handleFileChange.bind(this)
    this.handleTextChange=this.handleTextChange.bind(this)
    this.handleSubmit=this.handleSubmit.bind(this)
  }
  handleTextChange = (e) => {
    this.setState({ [e.target.name]: e.target.value })
  }

  handleFileChange = (e) => {
    console.log(e.target.files[0])
    this.setState({ file: e.target.files[0] })
  }

  handleSubmit = (e) => {
    const { text, file } = this.state

  }

  render() {
    return (
      <div className="App">
        <form action="" className="w-100" onSubmit={(e) => this.handleSubmit(e)}>
          <div className="pt-1 pb-3">
            <TextareaAutosize
              className="form-control"
              placeholder="write some text here"
              minRows="4"
              name="text"
              onChange={(e) => this.handleChange(e)}
            />
          </div>
          <div className="w-100 d-flex justify-content-between align-items-center">
            <div>
              <input
                type="file"
                className="form-control-file"
                name="file"
                onChange={(e) => this.handleFileChange(e)}
              />
            </div>
            <div>
              <button type="submit" className="btn btn-primary">
                Post
           </button>
            </div>
          </div>
        </form>
      </div>
    );
  }
}

export default App;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.