2

Here i'm trying to add multiple files to upload, but it is replacing in to new one and the response value is also showing empty. i dont understand what i'm doing wrong here thanks in advance

const [question, setQuestion] = React.useState('');
const [fileImage, setFileImage] = React.useState([]);

const handleChange = (event) => {
 setQuestion(event.target.value);
}

const handleSelectChange = (event) => {
 setFileImage(event.target.files);
}

addQuizDetails() {
  const Questions = question;
  const fileImages = fileImage;
  const headers = {
    "Content-Type": "multipart/form-data"
  }
  var formData = new FormData();
    formData.append("image", fileImages);
    formData.append("question",question )
    axios
    .post("http://localhost:4000/api/quiz/addQuiz",formData,headers)
    .then(response => {
      if (response.data.message) {
        alert(response.data.message)
      }
      else {
        alert("failed")
      }
  })
 }
}


 <TextField className={classes.content} onChange={handleChange} label="Question" variant="outlined" />
          <input 
            type="file" 
            onChange={handleSelectChange}
            multiple />
          <div className={classes.button}>
            <Button className={classes.submit} variant="contained" onClick={addQuizDetails}>Submit</Button>           
          </div>

1 Answer 1

1

Try below code by using spread operator for array:

const handleSelectChange = (event) => {
    const selectedFiles = event.target.files
    setFileImage([...fileImage, ...selectedFiles]);
}
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.