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();"GET", "/path/to/local/image/file", true);
xhr.responseType = "blob";
xhr.onload = function (e) {
var reader = new FileReader();
reader.onload = function(event) {
var res =;
var file = this.response;

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