gpt4 book ai didi

javascript - 如何在新标签页/窗口中编写文本文档?

转载 作者:行者123 更新时间:2023-11-30 17:06:58 25 4
gpt4 key购买 nike

如果扩展名为 jpg,下面的代码用于在新选项卡中显示图像。

同样,如果扩展名是txt,我想在新选项卡中写一个文本文档(从base64字符串到文本)

success: function (data) {
var extension = fileName.split('.').pop();
if (extension == "jpg") {
var image = new Image();
image.src = "data:image/jpg;base64," + data.d;
var w = window.open("");
w.document.write(image.outerHTML);
}
else if(extension == "txt") {

}
}

最佳答案

Base64 图像更简单,因为您只需将 base64 粘贴到图像源中,文本等其他内容需要解码。参见 https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

success: function (data) {
var extension = fileName.split('.').pop();
if (extension == "jpg") {
var image = new Image();
image.src = "data:image/jpg;base64," + data.d;
var w = window.open("");
w.document.write(image.outerHTML);
}
else if(extension == "txt") {
var w = window.open("");
w.document.write(window.atob(data.d));
}
}

关于javascript - 如何在新标签页/窗口中编写文本文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27813359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com