This question already has answers here:
Tainted canvases may not be exported
                                
                                    (10个回答)
                                
                        
                5年前关闭。
            
        

我试图获取给出其URL的图像的base64字符串表示形式。

HTML:

<input type="text" value="" id="urlFileUpload"/>
<input type="button" id="btn" value="GO" />


JS:

$('#btn').click(function()
{
    var img = new Image();
    img.src = $('#urlFileUpload').val();
    img.onload = function ()
    {
        var canvas = document.createElement("canvas");
        canvas.width = this.width;
        canvas.height = this.height;

        var ctx = canvas.getContext("2d");
        ctx.drawImage(this, 0, 0);

        console.log(canvas.toDataURL("image/png"));
    }
});


我收到此错误:Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

这是JSFiddle

最佳答案

这是因为您的本地驱动器被视为“其他域”,请尝试将其上传到Web服务器
它解决了我的问题

08-19 15:15