Tuesday, August 21, 2007

Implementing file download functionality

Following is the code for implementing file download functionality in the web application.

function filedownload()
{
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "application/doc";
System.IO.FileInfo myFile = new System.IO.FileInfo("c:\\MyDoc.doc"); Response.AppendHeader("Content-Length", myFile.Length.ToString()); Response.AppendHeader("content-disposition", "attachment; filename=" + System.IO.Path.GetFileName("c:\\MyDoc.doc"));
Response.WriteFile("c:\\MyDoc.doc");
Response.End();
}

In the above code snippet, a word document in the web server's C: drive is used for downloading.

No comments: