简体   繁体   中英

How to convert Base64 to image

I am trying to display image from database. I have converted the database image into base 64 format. I am using JavaScript for the conversion. But its not an image format. My code is as follows:

var che = btoa(data.d.ItemImage1);
$('#imgUpload').attr('src', "data:image/png;base64," + che);

On alerting this, the format will be as follows, but it cannot show an image.

MTAwLDk3LDExNiw5Nyw1OCwxMDUsMTA5LDk3LDEwMywxMDEsNDcsMTA2LDExMiwxMDEsMTAzLDU5LDk4LDk3LDExNSwxMDEsNTQsNTIsNDQsNDcsNTcsMTA2LDQ3LDUyLDY1LDY1LDgxLDgzLDEwNyw5MCw3NCw4MiwxMDMsNjUsNjYsNjUsMTAzLDY5LDY1LDg5LDY1LDY2LDEwMyw2NSw2NSw2OCw0Nyw1NSwxMDMsNjUsNzksODEsODcsODIsMTE4LDg5LDEwOSw4NSw2NSw5MCw2NSw2NSw2NSw2NSw2NSw2NSw2Niw0Nyw0Myw2OSw4Myw2Nyw2OSw4Niw1Miw5Nyw4Nyw4OSw2NSw2NSw2OSw0OSw3OCw2NSw2NywxMTEsNjUsNjUsNjUsNjUsNzMsNjUsNjUsOTksNjYsNzcsMTAzLDY1LDY3LDY1LDY1LDY1LDY1LDcwLDY1LDY1LDY1LDY1LDcxLDczLDY2LDc5LDExOSw2NSw2Nyw2NSw2NSw2NSw2NSw2NiwxMTksNjUsNjUsNjUsNzIsOTAsNzIsODIsMTAzLDY1LDY4LDY1LDY1LDY1LDY1LDY1LDgxLDY1LDY5LDY1LDY1LDY2LDcyLDgzLDgxLDY1LDY4LDY1LDY1LDY1LDY1LDY1LDgxLDY1LDQ3LDY1LDY1LDY3LDk5LDExMCw4MSw2NSw2Niw2NSw2NSw2NSw2NSw2OCwxMDMsNjUsNjUsNjUsNjUsNjgsMTEzLDcyLDY1LDY1LDcyLDY1LDY1LDY1LDcyLDk4LDEwMyw2NSw2NSw2NSw2NSw2Nyw3Miw5Nyw4MSw2NSw2OSw2NSw2NSw2NSw2NSw2NSw4MSw2NSw2NSw2NSw3Miw0OCw2NSw2NSw2NSw2OCwxMTAsNzcsMTA2LDY1LDExOSw3OSw4NCwxMTEsMTE5LDc3LDEyMiwxMTEsMTIwLDc3LDEwNSw2NSwxMjAsNzcsMTIyLDExMSw0OCw3OSw2OCwxMTEsMTIyLDc4LDgxLDY2LDY4LDk4LDUxLDc0LDEwNSw5Nyw4OCw3Nw== 

对于您的情况,请尝试以下

$('#imgUpload').attr('src', String.fromCharCode.apply(null, data.d.ItemImage1));

Use this:

$('#imgPhoto').attr('src', "data:image/jpeg;base64," + imageData); works fine. We are using this. Check your imageData value(in your case it is data.d.ItemImage1). Check the value using alert.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM