[英]How to open the newly created image in a new tab?
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);
}
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);
}
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 https://stackoverflow.com/a/16245768/8270748
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/2450431 https://stackoverflow.com/a/46510790/2450431 https://stackoverflow.com/a/27798235/2450431 https://stackoverflow.com/a/58615423/2450431 https://stackoverflow.com/a/46510790/2450431 https://stackoverflow.com/a/27798235/2450431
You can use document.write() and add html page by yourself.您可以使用document.write()并自行添加 html 页面。 This option allows also to add tab title.此选项还允许添加选项卡标题。
const newTab = window.open();
newTab?.document.write(
`<!DOCTYPE html><head><title>Document preview</title></head><body><img src="${base64File}" width="100px" height="100px" ></body></html>`);
newTab?.document.close();
Updated DEMO (working in Chrome, even with pops blocked) - 3/3/2021更新的演示(在 Chrome 中工作,即使弹出阻止) - 2021 年 3 月 3 日
wrapping the call to window.open
circumvents the issues detailed below.包装对window.open
的调用避免了下面详述的问题。
document.getElementById('my_button').addEventListener('click', (evt) => {
window.open("https://via.placeholder.com/150", '_blank')
});
Last tested in Chrome 88 on 64 bit Windows 10.最后在 64 位 Windows 10 上的 Chrome 88 中测试。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.