當我單擊 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
--編輯--我為gridview添加了我的html。
<asp:GridView style ="margin:10px" runat="server" ID="FileList" AllowPaging="True" PageSize="2" PagerSettings-Position="Top" PagerSettings-Mode="Numeric" AutoGenerateColumns="false" SelectedIndex="0">
<HeaderStyle BackColor="#3AC0F2" ForeColor="White"></HeaderStyle>
<Columns>
<asp:BoundField DataField="Num" HeaderText="#" ItemStyle-Width="20" HtmlEncode="false" ><ItemStyle Width="20px"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="Process" HeaderText="Process" ItemStyle-Width="200" HtmlEncode="false"><ItemStyle Width="200px"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="Datetime" HeaderText="Datetime" ItemStyle-Width="300" HtmlEncode="false"><ItemStyle Width="300px"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="InCharge" HeaderText="InCharge" ItemStyle-Width="150" HtmlEncode="false"><ItemStyle Width="150px"></ItemStyle></asp:BoundField>
<asp:TemplateField ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="btnDisplay" runat="server" Text="Display" Visible='<%# If(Eval("Process").ToString() = "アップロード", True, False) %>' OnClick="btnDisplay_Click" Target="_blank" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
uj5u.com熱心網友回復:
首先使用超鏈接而不是 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
uj5u.com熱心網友回復:
我認為你遺漏了一些東西,在 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上肯定有問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/381698.html
上一篇:asp.netc#多次提供引數“@stIdCity”
下一篇:.netcore第三方圖形驗證
