How to upload image or file in react js?

Viewed 48

I want to upload image / file in a react project, but dont want to use formdata to post, instead i want to use body for the post method through Axios. If I can convert my file into base 64, I think i will be able to post the image through body. Can anyone tell me solution how to achieve this?

1 Answers

Use this code :

function encodeImageFileAsURL(element) {
  var file = element.files[0];
  var reader = new FileReader();
  reader.onloadend = function() {
    console.log('RESULT', reader.result)
  }
  reader.readAsDataURL(file);
}

Related