zahra zamani
zahra zamani

Reputation: 1375

How to send data in FormData to api?

I want to send the data to the Api as FormData I use react react-hook-form This is what I have:

const submitForm = (data) => {
    console.log(data)==>{Title:"test" ,Countent:"countent"} 
    service.uploadFile(data, (status, result) => {
      if (result.Success) {
        props.notifySuccess(result.Message);
      } else {
        props.notifyError(result.Message);
      }
    });
  };

How do I send data as FormData?

Upvotes: 0

Views: 104

Answers (1)

Avani Bataviya
Avani Bataviya

Reputation: 770

const submitForm = (data) => {
     const formData = new FormData();
    Object.keys(data).forEach((ele) => formData.append(ele, data[ele]))
    service.uploadFile(formData, (status, result) => {
      if (result.Success) {
        props.notifySuccess(result.Message);
      } else {
        props.notifyError(result.Message);
      }
    });
  };

Upvotes: 1

Related Questions