简体   繁体   English

使用Javascript在新标签页中打开PDF

[英]Open PDF in new tab with Javascript

I am trying to open a PDF sitting on our sever in a new window when a button is clicked. 单击按钮时,我正在尝试在新窗口中打开坐在我们服务器上的PDF。 I am using the window.open() function in javascript to try and achieve this. 我正在使用javascript中的window.open()函数来尝试实现此目的。 When I click the button, a new tab opens, but it is an about:blank tab. 当我单击按钮时,将打开一个新标签,但这是一个about:blank标签。 Here is my ASP.NET markup for the button: 这是该按钮的ASP.NET标记:

<asp:Button ID="BTN_OpenPDF" runat="server" Text='<%#Eval("TicketNum").ToString()%>' OnClientClick='openPDF(this);'/>

And here is the javascript code that I am trying to execute: 这是我尝试执行的javascript代码:

function openPDF(element) {
        var text = element.value;

        window.open('Z:/Ticket Scans/' + text + '.pdf');
    }

I am dynamically setting the text of the button to retrieve the ticket number and this works fine. 我正在动态设置按钮的文本以检索票证编号,这可以正常工作。 When I step through the debugger I can tell that the file path is correct, but the pdf still does not open. 当我逐步通过调试器时,我可以知道文件路径正确,但是pdf仍然无法打开。

I am using ASP.NET, C#, and Chrome. 我正在使用ASP.NET,C#和Chrome。

Edit: From doing some research I believe that this may be a Chrome security issue. 编辑:经过研究,我认为这可能是Chrome安全问题。 When using an anchor tag instead of the Javascript: 当使用锚标记而不是Javascript时:

<a target="-blank" href="z:\Ticket Scans\<%#Eval("TicketNum") %>.pdf></a>

I get an error in the debugger saying: Now allowed to load local resource . 我在调试器中收到一条错误消息: Now allowed to load local resource But when I run the app in IE11 it works perfectly fine. 但是,当我在IE11中运行该应用程序时,它运行正常。

would like to create this icon for permanent use. 想要创建此图标以供永久使用。

Try adjusting command for launching chrome at properties of launcher icon , adding --allow-file-access-from-files flag 尝试在启动器图标的属性中调整用于启动chrome的命令,并添加--allow-file-access-from-files标志

/path/to/chrome --allow-file-access-from-files

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM