从图像URL获取Base64字符串

时间:2014-12-09 19:13:41

标签: javascript jquery image base64

我正在尝试获取图像的base64字符串表示,并给出其URL。

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

1 个答案:

答案 0 :(得分:0)

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

相关问题