提问人:Ravi Kumar 提问时间:10/4/2023 更新时间:10/10/2023 访问量:37
PDF 文件未在新选项卡中打开
PDF file is not getting opened in new tab
问:
我正在尝试在新选项卡中打开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
答:
0赞
Ravi Kumar
10/10/2023
#1
我改变了这一行
setTimeout(function () { window.document.forms[0].target = ''; }, 0);
对此
setTimeout(function () { window.document.forms[0].target = ''; }, 1);
现在它工作正常。
评论