S.M_Emamian
S.M_Emamian

Reputation: 17393

how to convert file input to base64 - react js

I have a file input and I want to convert it to base64 and send to server side. this is my code:

var avatar = (this.refs.avatar) ? this.refs.avatar.files : "" ;

if(avatar.length > 0){
  avatar = avatar[0]
  let fileReader = new FileReader();
  let file = null;
  fileReader.onload = function(fileLoadedEvent) {
       file = fileLoadedEvent.target.result;

   };
   fileReader.readAsDataURL(avatar);
}else{
  avatar = ""
}

console.log(avatar); //File(2468670) {name: "wood.png", lastModified: 1524227213060, lastModifiedDate: Fri Apr 20 2018 16:56:53 GMT+0430 (+0430), webkitRelativePath: "", size: 2468670, …}

should I covert avatar to base64? I should convert to origin file on server side.

Upvotes: 0

Views: 3197

Answers (1)

victor.ja
victor.ja

Reputation: 888

The atob() function decodes a string of data which has been encoded using base-64 encoding. Conversely, the btoa() function creates a base-64 encoded ASCII string from a "string" of binary data.

Upvotes: 1

Related Questions