PDF 文件未在新选项卡中打开

PDF file is not getting opened in new tab

提问人:Ravi Kumar 提问时间:10/4/2023 更新时间:10/10/2023 访问量:37

问:

我正在尝试在新选项卡中打开PDF文件,但它正在同一选项卡中打开。我经历了很多问题,但没有一个有效。

看起来 openInNewTab() 中的功能存在一些问题。

下面是 aspx 代码:


    <asp:TemplateField HeaderText="Attachment Name" SortExpression="Attachment_Name">
        <ItemTemplate>
            <asp:LinkButton ID="lnkAttachmentName" runat="server" CausesValidation="false" CommandName="View" CssClass="noline clsAttachmentName" Text='<%#Bind("Attachment_Name") %>' OnClientClick="openInNewTab();"></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>

openInNewTab() 函数:


    <script type="text/javascript">
    function openInNewTab() { 
        window.document.forms[0].target = '_blank';
        setTimeout(function () { window.document.forms[0].target = ''; }, 0);
        return true;
    }
    </script>

VB代码:


    Protected Sub gvAttachment_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAttachment.RowCommand
        Try
            If (e.CommandName = "View") Then
                Dim AttachmentID As Integer
                Dim row As GridViewRow = CType(CType(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
                AttachmentID = CInt(gvAttachment.DataKeys(row.RowIndex).Value.ToString())
                ViewState("AttachmentID") = AttachmentID
                LoadAttachment(AttachmentID)
                ModData.WriteViewLog(User_ID, "General Attachments", AttachmentID, "General Attachments", "View", Session.Item("SoftID"), 0)
            End If
        Catch ex As Exception
            Components.ExceptionLogger.HandleException(ex)
        End Try
    End Sub
    Private Sub LoadAttachment(ByVal AttachmentID As Integer)
            Try
                Dim FiletoDownload As String
                Dim MyFileInfo As FileInfo
                Dim FileSize As Long
                Dim strpath As String
                strpath = String.Empty
                objBL_PatientEdu = New BL_PatientEducation(Session.Item("SoftId"))
                Dim dt As DataTable = New DataTable()
                dt = objBL_PatientEdu.viewAttachmentByID(AttachmentID)
                If dt.Rows.Count > 0 Then
                    strpath = dt.Rows(0)("Attachment_Location").ToString() 
                End If
                If File.Exists(strpath) Then
                    FiletoDownload = strpath
                    MyFileInfo = New FileInfo(FiletoDownload)
                    FileSize = MyFileInfo.Length
                    Dim filename As String = Path.GetFileName(strpath)
                    Response.Clear()
                    Response.ClearContent()
                    Response.ClearHeaders()
                    Response.ContentType = "Application/pdf"
                    Response.AppendHeader("Content-Disposition", "inline; filename=""" & filename & """")
                    Response.AppendHeader("Content-Length", FileSize.ToString())
                    Response.Buffer = True
                    Response.BinaryWrite(System.IO.File.ReadAllBytes(strpath))
                    Response.End()
                End If
            Catch ex As Exception
                Components.ExceptionLogger.HandleException(ex)
            End Try
        End Sub

JavaScript asp.net vb.net

评论

1赞 VDWWD 10/4/2023
您无法控制 pdf 是否在新选项卡中打开,这取决于用户及其浏览器设置。
0赞 This Guy 10/4/2023
window.document.forms[0].target = '_blank';setTimeout(函数 () { window.document.forms[0].target = ''; }, 0);目标应该只为“_blank”developer.mozilla.org/en-US/docs/Web/HTML/Element/a 它看起来你设置正确,但随后将其设置为当前窗口......
0赞 Ravi Kumar 10/5/2023
@ThisGuy 如果我注释 setTimeout(function () { window.document.forms[0].target = ''; }, 0);然后在新选项卡中打开 pdf,但是现在所有其他调用(如 Response.Redirect)也在新选项卡中打开,这是我不想要的。
0赞 This Guy 10/5/2023
好的,我建议你研究并写下你的工作流程。尝试了解哪些项目需要在新选项卡中(您现在可以始终如一地执行此操作)以及哪些项目需要在当前选项卡中打开(或者您想做的任何其他方式)。试着了解这些行为的条件是什么。您可以创建另一个函数:例如,openInCurrentTab()...。

答:

0赞 Ravi Kumar 10/10/2023 #1

我改变了这一行

setTimeout(function () { window.document.forms[0].target = ''; }, 0);

对此

setTimeout(function () { window.document.forms[0].target = ''; }, 1);

现在它工作正常。