Saturday, September 13, 2008

How to flush the word & excel file from the web site, using C#

System.IO.FileInfo file;

here sFile = your virtual file path (/myfiles/abc.xls)

file = new System.IO.FileInfo(sFile);
if (file.Exists)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;
filename=" + file.Name);

HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.End();
System.GC.Collect();
}
else
{
HttpContext.Current.Response.Write("This file does not exist.");
}

No comments: