【问题标题】:Get Base64 string from image URL [duplicate]从图像 URL 获取 Base64 字符串 [重复]
【发布时间】:2015-02-07 19:39:05
【问题描述】:

我正在尝试获取给出其 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。

【问题讨论】:

    标签: javascript jquery image base64


    【解决方案1】:

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

    【讨论】:

    • 我不明白为什么本地驱动器有问题。我试图访问的文件已上传到网络上。 $('#urlFileUpload').val(); 类似于 http://someimage.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多