如何在新标签中打开新创建的图像?

时间:2015-01-06 11:53:42

标签: javascript jquery ajax dom

下面的代码会在同一页面的底部创建图像。 如何将该图像显示到新的选项卡/窗口而不是显示在同一页面中?

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

5 个答案:

答案 0 :(得分:36)

类似的东西:

success: function (data) {
        var image = new Image();
        image.src = "data:image/jpg;base64," + data.d;

        var w = window.open("");
        w.document.write(image.outerHTML);
    }

答案 1 :(得分:4)

demo

window.open(image.src, '_blank');

答案 2 :(得分:3)

当前建议不会让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');

感谢杰里米!
https://stackoverflow.com/a/16245768/8270748

答案 3 :(得分:1)

最新解决方案-将于2019年10月发布。

在新标签页中打开图像。

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

答案 4 :(得分:0)

如果您只想在新标签页中查看图片,这可以正常工作。

let data = 'data:image/png;base64,iVBORw0KGgoAAAANS';
window.open(data);