CORS failed to convert an image

1

I have a variable that returns an url of an image:

   var imagem = $scope.authentication.user.imagem.toString();

I want to pass this variable to another method, where I want to encode for base64:

 getDataUri(imagem, function (dataUri) {
            logo = dataUri;
            console.log("logo=" + logo);
        });

   function getDataUri(url, cb)
    {
        var image = new Image();
        //image.crossOrigin = 'anonymous';
        image.setAttribute('crossOrigin', 'anonymous');

        image.onload = function () {
            var canvas = document.createElement('canvas');
            canvas.width = this.naturalWidth;
            canvas.height = this.naturalHeight;

            var ctx = canvas.getContext('2d');
            ctx.fillStyle = '#fff'; 
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            canvas.getContext('2d').drawImage(this, 0, 0);

            cb(canvas.toDataURL('image/jpeg'));
        };

        image.src = url;
    }

This function returns the following error:

Access to Image at 'https://URL-DA-VARIAVEL/7367506d-9615-4131-8e13-9c13e549e48d.jpeg' from origin 'http://localhost:15495' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:15495' is therefore not allowed access.

A CORS error. But in my getDataUri function, I'm setting: image.setAttribute('crossOrigin', 'anonymous');

I need to get the hash of the converted image to be able to insert into a PDF.

    
asked by anonymous 19.07.2018 / 19:34

0 answers