Convert local URL(URL) to Base64 in javascript

Shivam Gupta
1 min readNov 19, 2019
  1. Use XMLHttpRequest()
  2. set responseType to Blob ,
  3. use FileReader() at XMLHttpRequest onload event to read response as data URI
var xhr = new XMLHttpRequest();       
xhr.open("GET", "/path/to/local/image/file", true);
xhr.responseType = "blob";
xhr.onload = function (e) {
console.log(this.response);
var reader = new FileReader();
reader.onload = function(event) {
var res = event.target.result;
console.log(res)
}
var file = this.response;
reader.readAsDataURL(file)
};
xhr.send()

Thanks! Please give a clap if the post is helpful. Write in the comment section for any help or suggestion.

--

--

Shivam Gupta

Full Stack Engineer (Web/App) working on different JS Technologies & frameworks— Angular, Node, Typescript, Ionic, Firebase, AWS, ElK...Love to write cool stuff