简体   繁体   中英

How to show Base64 image on browser's new tab?

I have Base64 encoded image as response, how can I show that image in new tab of browser using js? anybody can suggest a solutions. Thanks

 success: function (base64Image) {


         }

Supposing you are going to get a GIF image:


// Display a base64 URL inside an iframe in another window.
function debugBase64(base64URL){
    var win = window.open();
    win.document.write('<iframe src="' + base64URL  + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
}

success: function (base64Image) {

    // e.g This will open an image in a new window
    debugBase64("data:image/gif;base64," + base64Image);

    // you can try with this base64 (it is a red dot)
    // debugBase64("data:image/gif;base64,R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs=");

}

Otherwise just change data:image/gif; with the right MIME type you need.

You can find a complete list here https://www.freeformatter.com/mime-types-list.html

I found and I just tried this solution from https://ourcodeworld.com/articles/read/682/what-does-the-not-allowed-to-navigate-top-frame-to-data-url-javascript-exception-means-in-google-chrome

let image = new Image();
image.src = "you-base64-encoded-image";
var newTab = window.open();
newTab.document.body.innerHTML = image.outerHTML;

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