在其他页面查看pdf文件

本文关键字:pdf 文件 其他 | 更新日期: 2023-09-27 18:03:38

这里有查看pdf文件的代码,目前该文件可以在同一页面中查看。我想在新的页面显示文件时,链接按钮点击在gridview。据了解,文字嵌入链接需要改变,但我不确定。我的问题是如何在新页面中查看文件?

Gridview代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="comName" HeaderText="Company Name" SortExpression="comName" />
            <asp:BoundField DataField="sName" HeaderText="Stock Name" SortExpression="sName" />
            <asp:BoundField DataField="annDate" HeaderText="Date Announced" SortExpression="annDate" />
            <asp:BoundField DataField="fYearEnd" HeaderText="Financial Year End" SortExpression="fYearEnd" />
            <asp:BoundField DataField="quarterr" HeaderText="Quarter" SortExpression="quarterr" />
            <asp:BoundField DataField="QFREDate" HeaderText="Financial Period Ended " SortExpression="QFREDate" />
            <asp:BoundField DataField="figure" HeaderText="Figure" SortExpression="figure" />
            <asp:TemplateField ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="View" CommandArgument='<%# Eval("id") %>'></asp:LinkButton>
        </ItemTemplate>

view的后台代码:

 protected void View(object sender, EventArgs e)
    {
        int id = int.Parse((sender as LinkButton).CommandArgument);
        string embed = "<object data='"{0}{1}'" type='"application/pdf'" width='"100%'" height='"100%'">";
        embed += "If you are unable to view file, you can download from <a href = '"{0}{1}&download=1'">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("~/FileCS.ashx?Id="), id);
    }

FileCS。ashx代码:

 public void ProcessRequest(HttpContext context)
{
    int id = int.Parse(context.Request.QueryString["Id"]);
    byte[] bytes;
    string fileName, contentType;
    string constr = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "SELECT  fName, contentType, data FROM announ WHERE Id=@Id";
            cmd.Parameters.AddWithValue("@Id", id);
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                sdr.Read();
                bytes = (byte[])sdr["data"];
                contentType = sdr["contentType"].ToString();
                fileName = sdr["fName"].ToString();
            }
            con.Close();
        }
    }
    context.Response.Buffer = true;
    context.Response.Charset = "";
    if (context.Request.QueryString["download"] == "1")
    {
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    }
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    context.Response.ContentType = "application/pdf";
    context.Response.BinaryWrite(bytes);
    context.Response.Flush();
    context.Response.End();
}

在其他页面查看pdf文件

在您的链接按钮添加OnClientClick="aspnetForm。目标="平等";"

<asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="View" OnClientClick="aspnetForm.target ='_blank';" CommandArgument='<%# Eval("id") %>'></asp:LinkButton>

这将打开一个新的选项卡