Handle Uncaught Security Exception in HTML toDataURL



To solve the Uncaught Security Exception, you need to add the crossorigin attribute:

<img id = "myid" crossorigin = "anonymous" src="https://www.tutorialspoint.com/images/seaborn-4.jpg?v=2">

function getBase64() {
   var myImg = document.getElementById("myid");
   var c = document.createElement("canvas");

   c.width = myImg.width;
   c.height = myImg.width;

   var context = c.getContext("2d");
   context.drawImage(img, 0, 0);

   var dataURL = c.toDataURL("image/png");
   alert(dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
}
getBase64();
Updated on: 2020-01-29T06:24:50+05:30

113 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements