Downloads.aspx
<asp:GridView ID="gvDownloads" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Brochures" HeaderStyle-BackColor="Gainsboro">
<ItemTemplate>
<asp:Label ID="lblfile" width="400px" Text='<%#DataBinder.Eval(Container.DataItem,"filename") %>' runat="server"></asp:Label>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("id", "~/Download.aspx?id={0}") %>'
Text="Download"></asp:HyperLink>
<asp:HyperLink ID="lnkView" runat="server" NavigateUrl='<%# Eval("id", "~/View.aspx?id={0}") %>' Text="View"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Downloads.aspx.cs
public void display()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from downloads", con);
da.Fill(ds);
gvDownloads.DataSource = ds;
gvDownloads.DataBind();
con.Close();
}
Download.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Web.Mail;
using System.IO;
public partial class Download : System.Web.UI.Page
{
public SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SLMFConnection"].ToString());
public string conStr = ConfigurationManager.ConnectionStrings["SLMFConnection"].ToString();
SqlCommand cmd;
int cid;
byte[] b;
protected void Page_Load(object sender, EventArgs e)
{
int docId = Convert.ToInt32(Request.QueryString["id"]);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Select * from downloads where id = @id";
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters[0].Value = docId;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
b = ((byte[])(dr["contentfile"]));
Response.Clear();
Response.ContentType = dr["contenttype"].ToString();
Response.AppendHeader("Content-Disposition", ("attachment; filename=\""
+ (dr["filename"] + "\"")));
Response.BinaryWrite(b);
}
con.Close();
}
}
No comments:
Post a Comment