I am trying to get image data uri through image url using the function below, however, I kept getting a cross origin error even though the image is stored in the localhost folder. Can anyone help me with this?
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function () {
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = this.naturalHeight;
canvas.width = this.naturalWidth;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
var imageAttribute = {
dataUrl: dataURL,
height: canvas.height,
width: canvas.width
};
if (callback) callback(imageAttribute, options);
};
img.src = url;
console.log(img.src);
The code throws cross origin issue on the line img.src = url
as it tries to hit my localhost url.
Below is the error that is shown in the console.
Access to image at 'https://localhost:44377//Data/files/logo-rs.png' from origin 'https://localhost:44376' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.