Javascript 如何在新选项卡中打开新创建的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27798126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open the newly created image in a new tab?
提问by John Stephen
Below code creates the image in the bottom of the same page. How to display that image into a new tab/window instead of displaying in the same page?
下面的代码在同一页面的底部创建图像。如何将该图像显示到新选项卡/窗口中而不是显示在同一页面中?
success: function (data) {
var image = new Image();
image.src = "data:image/jpg;base64," + data.d;
document.body.appendChild(image);
}
回答by Super Babaca
something like:
就像是:
success: function (data) {
var image = new Image();
image.src = "data:image/jpg;base64," + data.d;
var w = window.open("");
w.document.write(image.outerHTML);
}
回答by crazyx13th
current suggestions not worling at chrome, I always getting a white page, now I using
当前的建议不在 chrome 上,我总是得到一个白页,现在我使用
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 to Jeremy!
https://stackoverflow.com/a/16245768/8270748
回答by hrvoj3e
Latest solution - works 2019-10.
最新解决方案 - 2019-10 有效。
Open image in new tab.
在新选项卡中打开图像。
let data = 'data:image/png;base64,iVBORw0KGgoAAAANS';
let w = window.open('about:blank');
let image = new Image();
image.src = data;
setTimeout(function(){
w.document.write(image.outerHTML);
}, 0);
https://stackoverflow.com/a/58615423/2450431https://stackoverflow.com/a/46510790/2450431https://stackoverflow.com/a/27798235/2450431
https://stackoverflow.com/a/58615423/2450431 https://stackoverflow.com/a/46510790/2450431 https://stackoverflow.com/a/27798235/2450431

