How to display base64 encoded image in HTML if it is located in a separated file?

后端 未结 4 975
悲哀的现实
悲哀的现实 2020-12-14 12:02

I have base64 encoded image. If I put it right into html it works:


But when I put all that b

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 12:27

    I did something similar for my final year project at university, i was using XML doc's to link to a page and show the images in a canvas element. I made it so the image was searched for a variable, then assigned the variable with base 64 encoded image like so:

    xmlDoc=loadXMLDoc("test.xml");
    
    x=xmlDoc.getElementsByTagName("image");
    txt=x[1].childNodes[0].nodeValue;
    document.write(txt);
    
    var card1 = txt;
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    var img=new Image();
    img.onload = function(){
        ctx.drawImage(img,0,0);
    };
    img.src= card1;
    

提交回复
热议问题