Search code examples
reactjsreact-hooksform-datareact-hook-form

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?


Solution

  • 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);
          }
        });
      };