Thursday, November 12, 2009

Download a file

While working with a scenario, I came across a situation to make the users to download a file. To download a file we can have the following code

try
{
string[] file = anrDownloadPdf.HRef.ToString().Split(Convert.ToChar("/"));


string strUpdPath = System.Configuration.ConfigurationManager.AppSettings["FileUploadPath"];
if (File.Exists(Server.MapPath(strUpdPath + file[file.GetUpperBound(0)])))
{

Response.ContentType = "Application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=application.pdf");
Response.TransmitFile(anrDownloadPdf.HRef);

// Response.End(); Don’t use Respons.End here since itcan have the thread abortion exception
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
catch (Exception ex)
{

}

No comments:

Post a Comment

...

Obstacles are those frightful things you see when you take your eyes off your goal.------> by Henry Ford