gpt4 book ai didi

javascript - 使用 Javascript 在带有自定义磁贴的新浏览器选项卡中打开 PDF blob 文件

转载 作者:行者123 更新时间:2023-12-05 07:10:55 26 4
gpt4 key购买 nike

我正在尝试使用 Javascript 在新选项卡中打开 PDF 文件。除了选项卡标题外,它工作正常。选项卡标题显示一些随机 GUID。我使用浏览器 Chrome。

代码:

var myBlob= new Blob([blob], {type: ‘application/pdf’})
var file = new File([myBlob], "fileName.pdf");
const data = window.URL.createObjectURL(myBlob)
window.open(data)// it is opening the pdf in new tab but tab name is random GUID

所以我有不同的方法:

  1. // it is opening the pdf in new tab but tab name is random GUID`
    window.open(data,"my custom tab name")
  2. // still no luck`
    window.open(data,"my custom tab name").document.title = "New Page Title!";
  3. // Still not working
    var newWindow = window.open(data,'_blank');
    setTimeout(function () {
    newWindow.document.title = "My Tab Name";
    }, 100);
  4. // still not working
    var newWindow = window.open(data, "_blank");
    newWindow.addEventListener('load', function() {
    newWindow.document.title = 'New Title';
    });
  5. // downloading file not showing in new tab
    var a = document.createElement('A');
    a.href = data;
    a.download = data;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);

所以用了所有的方法。我可以在新标签页中显示 PDF,但无法设置标签页标题。

谁能帮我解决这个问题?

最佳答案

类似这样的东西:

const newWindow = window.open()
newWindow.document.title = 'My Tab Name'
newWindow.document.write`<iframe src="${URL.createObjectURL(myBlob)}" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>`)

关于javascript - 使用 Javascript 在带有自定义磁贴的新浏览器选项卡中打开 PDF blob 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61047746/

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