Hi nedash,
I have modified in your code.So please refer the below code.
C#
private string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
this.GetRecord();
}
private void GetRecord()
{
string downloadtoken = Page.RouteData.Values["downloadtoken"].ToString();
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("SELECT * FROM download WHERE DownloadToken='" + downloadtoken + "'", con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["ExpireAfterDownload"].ToString().ToUpper() == "TRUE")
{
if (dt.Rows[0]["Downloaded"].ToString().ToUpper() == "TRUE")
{
Response.Redirect("~/DownloadError.aspx");
}
}
else
{
if (Convert.ToDateTime(dt.Rows[0]["ExpiryDate"].ToString()) < DateTime.Now)
{
Response.Redirect("~/DownloadError.aspx");
}
}
string path = Server.MapPath(dt.Rows[0]["Url"].ToString());
FileStream fs = File.OpenRead(path);
byte[] fileData = new byte[fs.Length];
fs.Read(fileData, 0, (int)fs.Length);
Response.Clear();
Response.AddHeader("Content-Type", "application/zip");
Response.AddHeader("Content-Disposition", "inline;filename=" + Path.GetFileName(path));
Response.BinaryWrite(fileData);
Response.Flush();
Response.Close();
if (dt.Rows[0]["ExpireAfterDownload"].ToString().ToUpper() == "TRUE")
{
dt.Rows[0]["Downloaded"] = "true";
dt.Rows[0]["Hits"] = Convert.ToInt32(dt.Rows[0]["Hits"]) + 1;
dt.AcceptChanges();
}
}
}