0

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.

7
  • 1
    Why do you need to convert it? Uploading it will convert it to the proper MIME Type. The server can handle it from there. Commented May 10, 2018 at 13:46
  • My web-service require a base64 string parameter. Commented May 10, 2018 at 13:49
  • your code does the job, what's the matter?, file is a base64 representation of avatar Commented May 10, 2018 at 13:53
  • how to convert to base64? Commented May 10, 2018 at 13:55
  • should I convert file to base64 or avatar? Commented May 10, 2018 at 13:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.