Tuesday, December 11, 2007

Download file

public void downloadfile(int fileid)
{

sqlcom = new SqlCommand("select filename from NewsMaster where NewsID=" + fileid, sqlcon);
reader = sqlcom.ExecuteReader();
if (reader.Read())
{

string filename = Server.MapPath("NewsDownload" + @"\" + fileid + @"\" + reader["filename"].ToString());
downloadmethod(filename);

}
reader.Close();

}
public void downloadmethod(string filename)
{
if (filename != "")
{

string path = filename;

System.IO.FileInfo file = new System.IO.FileInfo(path);

if (file.Exists)
{

Response.Clear();

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

Response.AddHeader("Content-Length", file.Length.ToString());

Response.ContentType = "application/octet-stream";

Response.WriteFile(file.FullName);

Response.End();

}



}

}

No comments: