在javascript window.open上设置标签标题以显示PDF文件

mel*_*182 1 html javascript pdf google-chrome window.open

打开一个新标签来显示PDF文件.我在bytearray,base 64上有PDF文件数据.

我能够获取数据,并显示它:

downloadFile(strData, name) {
    var newdata = "data:" + "application/pdf" + ";base64," + (strData);
    var newWindow = window.open(newdata, "_blank");
    newWindow.onload = function() { document.title = "My title"; }
return true;
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是我无法将标题设置为打开的新选项卡.

我想设置一个标题,如"PDF文件"或只是文档的名称(我分别获取文件数据和文件名,并将其传递给我的downloadFile函数.

有没有办法将标题设置为此选项卡?提前致谢!

Ste*_* B. 7

试试这个:

  ....
  var newWindow = window.open(newdata, "_blank");
  newWindow.document.title = "Some title";
  ....
Run Code Online (Sandbox Code Playgroud)

编辑:

另一种方法是将iframe发送到新窗口,而不是直接使用base64字符串打开它.

所以类似于:

var newWindow = window.open();
newWindow.document.write('<iframe src="data:application/pdf;base64,' + (strData) + '" frameborder="0" allowfullscreen></iframe>');
newWindow.document.title = "Your Title Here";
Run Code Online (Sandbox Code Playgroud)