base64图像不显示在图像中

时间:2017-11-25 16:21:13

标签: javascript jquery html image

嗨我正在使用jquery图像上传器,在选择图像时,我得到它的base64代码。

function readImage(input) {
    if ( input.files && input.files[0] ) {
        var FR= new FileReader();
        FR.onload = function(e) {
            $('#img').show().attr( "src", e.target.result );
            $('#base').text( e.target.result );
        };
        FR.readAsDataURL( input.files[0] );
    }
}
$("#upload").change(function(){
    readImage( this );
});

我将base64编码存储到我的数据库中并在另一个页面上检索它我从表中获取以下数据

"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIABAMAAAAGVsnJAAAAIVBMVEUAAAB fX1 fX1 fX1 fX1 fX1 fX1 fX1 fX1 fX1 fX1I2PRsAAAACnRSTlMAF/ClME Kb9vEsIrXWQAACWpJREFUeNrs3T1rVEEUBuBzs1 JlbGImkpREW6lVrqVhBBCKhESIZWCIqTSgEZSKSrCVordVrrxY/P SouEJG7uzH7k3rBz3vf5CYe9Z87MOTNrIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiMo755fWdty931pfnjU/25EGOI73vby4akWzjPk75 IIlBtlGF4X2OUKw0kXQ/nPzrnEPUTcemWsrOYboef4RZO8wgi9uM0Gri5HsvzKXWh2MqO8yApdzjKz32txZyDGG3jNzZiEHmCPQyjGm3lNzpNHB2PqOSqKsjQns akHtjGR2 bEKib02VyoYWJ3zYF6BxPrP7T0HSYA2jRQAwDij DgAyD CLYBgPgjqOHM7ljKujizfUvYVZTgmyUr66AE/XT3BKsoxSdLVD1HKXqpLoWPUZJblqQGSpPm2cgSSnPTEnSQAYizwBWU6IMl57gGIK0F5lCqr5aaLk4g3BHU8B  TeEuSvbXklJH6dJaCQ/XQN6VsI3S7VlCWqhASmMjSzhCuSE4UQVyVoPHRQBpKbCJSvy2VHRQib4looUjnOvAJVTkuqWhjRMIa6EGBrAdjs6iMu8tBVuozB9LQIYKpVAMNlGhFOZnBxdBuoVwMAWwJYEsR4V6058EmhjAlgROpQC2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ZcJYHAS3xjDWO1hbMNAiBs A0DyJnwb95ELUW/FsPohqCCS wZQDzN2wZMBUCuMXwv4IYsS22Ou0xFgitAKyDpkoIWQcxPyBoHcR8EFsHNauENrBAvtIKiJ3hGd xhWAiBQHnoxYnpWANsT9MsJXwVAvjOoKJK4g5ITenS6DTITMG2KUA8wMCnBNPc10XQBdAY4BmAYD7w8qIu1oLqB8AnQaua2OkQbaxON7TlJY9Lfj/HiFcLywTxg oYXqiViA RI3TufeKhbD/84AURVEURVEURVEURVEURVEURVEURVEURVEURVEURVEURVEURVEURfndHhyQAAAAAAj6/7ofoQIAAAAAAAAAAPwEGcG4SMHdcSkAAAAASUVORK5CYII="

现在它想在我的图片标签中显示图像,我正在编写以下代码

$('#img').show().attr( "src", convert[0].profile_picture);

我在convert [0] .profile_picture变量中获取代码。

但图像未显示。截图是 screenshot of image tag in html

我提到其他问题并尝试使用

    var data = btoa(convert[0].profile_picture);
 $('#img').show().attr( "src", data );

但结果是一样的。在btoa中我是否必须删除数据:image / png; base64,然后尝试转换它?我尝试了一些在线base64进行图像转换,如

i used this link to try and covert base64 to image

但是我得到了无效的mime类型:application / octet-stream error。

我做错了什么?

更新: - 当我从数据库获取数据并在图像标记的src属性中使用它而不是在选择图像并预览图像之后,会出现问题。

1 个答案:

答案 0 :(得分:0)

非常感谢@Intervalia,我查看了我的代码,发现发送数据时javascript正在替换少数字符。所以我纠正了我的代码并使用了

var image=encodeURIComponent(document.getElementById("base").innerHTML);

然后将图像发送到数据库。当我检索它时,它开始显示。

再次感谢。