How to open a newly created image in a new tab?

Below the code creates an image at the bottom of the same page. How to display this image in a new tab / window instead of displaying on one page?

success: function (data) { var image = new Image(); image.src = "data:image/jpg;base64," + data.d; document.body.appendChild(image); } 
+10
source share
3 answers

sort of:

 success: function (data) { var image = new Image(); image.src = "data:image/jpg;base64," + data.d; var w = window.open(""); w.document.write(image.outerHTML); } 
+29
source

demonstration

 window.open(image.src, '_blank'); 
+3
source

current offers don't worry about Chrome, I always get a white page, now I use

 const base64ImageData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; const contentType = 'image/png'; const byteCharacters = atob(base64ImageData.substr('data:${contentType};base64,'.length)); const byteArrays = []; for (let offset = 0; offset < byteCharacters.length; offset += 1024) { const slice = byteCharacters.slice(offset, offset + 1024); const byteNumbers = new Array(slice.length); for (let i = 0; i < slice.length; i++) { byteNumbers[i] = slice.charCodeAt(i); } const byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } const blob = new Blob(byteArrays, {type: contentType}); const blobUrl = URL.createObjectURL(blob); window.open(blobUrl, '_blank'); 

thanks Jeremy!
fooobar.com/questions/24226 / ...

0
source

Source: https://habr.com/ru/post/980586/


All Articles