如何在asp.net(vb)的新标签中打开pdf文件?
Posted
技术标签:
【中文标题】如何在asp.net(vb)的新标签中打开pdf文件?【英文标题】:How to open pdf file in new tab in asp.net(vb)? 【发布时间】:2022-01-16 17:18:48 【问题描述】:当我单击gridview 的按钮时,我想在新选项卡中显示我的pdf 文件。如何显示?请有人帮助我。 这是我的代码。
'my gridview button click event
Protected Sub btnDisplay_Click(sender As Object, e As EventArgs)
Dim grdrow As GridViewRow = CType((CType(sender, Button)).NamingContainer, GridViewRow)
'I save pdf with datetime but showing file name without datetime on screen so I need to
'combine again when I need to open the file from upload folder
Dim dtime As DateTime = grdrow.Cells(2).Text
Dim fname As String = lblFileName.Text.Split(".").First + "_" +
dtime.ToString("yyyyMMddHHmmss") + ".pdf"
Dim FilePath As String = Server.MapPath("~/uploads/" & fname)
Dim User As WebClient = New WebClient()
Dim FileBuffer As Byte() = User.DownloadData(FilePath)
If FileBuffer IsNot Nothing Then
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", FileBuffer.Length.ToString())
Response.BinaryWrite(FileBuffer)
End If
End Sub
--编辑-- 我有了一些想法,它确实对我有用。 我添加了一些脚本来打开新标签。
html,网格视图
//javascript
<script type="text/javascript">
function openInNewTab()
window.document.forms[0].target = '_blank';
setTimeout(function () window.document.forms[0].target = ''; , 0);
</script>
<asp:BoundField DataField="FileName" HeaderText="Filename" ItemStyle-Width="200" HtmlEncode="false"><ItemStyle Width="200px"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="Process" HeaderText="Process" ItemStyle-Width="200" HtmlEncode="false"><ItemStyle Width="200px"></ItemStyle></asp:BoundField>
<asp:TemplateField ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="btnDisplay" runat="server" Text="Display" OnClick="btnDisplay_Click" OnClientClick="openInNewTab();" Visible='<%# If(Eval("Process").ToString() = "Uploaded", True, False) %>'></asp:Button>
</ItemTemplate>
</asp:TemplateField>
Main.aspx
Protected Sub btnDisplay_Click(sender As Object, e As EventArgs)
Dim grdrow As GridViewRow = CType((CType(sender, Button)).NamingContainer, GridViewRow)
Dim fname As String = grdrow.Cells(2).Text
'pdf Display
Session("pdfname") = fname
Response.Redirect("GeneratePDF.aspx")
End Sub
生成PDF.aspx
<form id="form1" runat="server">
<div style ="Display: Inline-block;float: left;">
<asp:Literal ID="ltEmbed" runat="server" />
</div>
</form>
生成PDF.aspx.vb
Dim pdf_name As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Page.Title = "PDF DISPLAY"
pdf_name = Session("pdfname")
Dim embed As String = "<object data=""0"" type=""application/pdf"" 2000px"" 1000px"">"
embed += "If you are unable to view file, you can download from <a href = ""0"">here</a>"
embed += " or download <a target = ""_blank"" href = ""http://get.adobe.com/reader/"">Adobe PDF Reader</a> to view the file."
embed += "</object>"
ltEmbed.Text = String.Format(embed, ResolveUrl("~/uploads/" + pdf_name))
End Sub
【问题讨论】:
【参考方案1】:我认为你遗漏了什么,在 if 语句的末尾添加 Response.End()
If FileBuffer IsNot Nothing Then
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", FileBuffer.Length.ToString())
Response.BinaryWrite(FileBuffer)
Response.End()
End If
如果它不起作用,那么Html肯定有问题
【讨论】:
洛伦佐,谢谢你的回答。我添加了 Response.End( ) 但仍然无法打开新标签。我还编辑了我的问题以添加我的 html 代码。 @Chang 不错!检查 MrinmoyeeG 的答案。它应该适合你。如果它不起作用,请记住您可以添加一个 pdf 阅读器,您可以使用<asp:LinkButton>
来获取它。你可以在这里找到一个有效的例子:aspsnippets.com/Articles/…
谢谢!当我看到你的例子时我明白了,它确实有效。【参考方案2】:
首先使用超链接而不是Button,并使目标=_blank。然后在新 aspx 的页面加载上编写生成 PDF 的代码。
'HTML CODE
<asp:HyperLink ID="btnDisplay" runat="server" Text="Open PDF" Target ="_blank" NavigateUrl="~/WBP/GeneratePDF.aspx"></asp:HyperLink>
'SERVER CODE
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim dtime As DateTime = DateTime.Now.ToString()
Dim fname As String = "pdffile_" +
dtime.ToString("yyyyMMddHHmmss") + ".pdf"
Dim FilePath As String = Server.MapPath("writereaddata/" & fname)
Dim User As WebClient = New WebClient()
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Dim pdfDoc As New Document(PageSize.A4, 10, 10, 8, 2)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream)
Dim sr As New StringReader(sw.ToString())
pdfDoc.Open()
Dim pthd As Paragraph = New Paragraph("WELCOME TO PDF FILE", New Font(Font.TIMES_ROMAN, 11, Font.BOLD, Color.BLACK))
pdfDoc.Add(pthd)
htmlparser.Parse(sr)
pdfDoc.Close()
HttpContext.Current.Response.Write(pdfDoc)
HttpContext.Current.Response.End()
End Sub
【讨论】:
感谢您的回答。我正在使用 vs 2019,有些代码对我来说效果不佳。可能我对此很陌生。我确实找到了一些打开新标签的方法。所以我编辑了我的问题并添加了对我有用的代码。再次感谢你。以上是关于如何在asp.net(vb)的新标签中打开pdf文件?的主要内容,如果未能解决你的问题,请参考以下文章