在另一页面中查看pdf文件

时间:2015-07-21 06:53:11

标签: c# html asp.net

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

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>

视图背后的代码:

 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();
}

1 个答案:

答案 0 :(得分:0)

链接按钮中的

添加OnClientClick =“aspnetForm.target ='_ blank';”

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

这将打开一个新标签。