0

I started learning react recently and I was trying to upload an image from a form to the api but I got error 415, I can upload the image from postman but I dont know how to do it from an imput. this is postman code

var formdata = new FormData();
formdata.append("file", fileInput.files[0], "/C:/Users/carlo/Downloads/descarga.jpg");

var requestOptions = {
  method: 'POST',
  body: formdata,
  redirect: 'follow'
};

fetch("http://localhost:8080/api/images", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

and this is mine

<label>
  <input
    class="form-control"
    type="file" id="formFile"
    onChange={(e)=>{sendimage(e)}}>
  </input>
</label>
function sendimage(a){
  const sending = a.target.files[0];
  
  var formData = new FormData();
  
  formData.append("file", a.target.files[0], "/C:/Users/carlo/Downloads/descarga.jpg");
  
  const setingsu=settings.url;
    const setingsp=settings.puerto;
    var url = setingsu+setingsp+"/api/images";
  fetch(url, {
    headers: { "Content-type": "application/json" },
    method: "POST",
    body: formData,
    
  }).then(response => console.log(response))
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
  
}
Phil
  • 157,677
  • 23
  • 242
  • 245

0 Answers0