Friday, February 13, 2009

Download a file from server

using System.IO;

string filename = "resumexavier1.doc";

if (filename != "")
{

string path = Server.MapPath(".\\Files\\" + filename);
// Here resumexavier is in "Files" folder,thats y given ".\\Files\\"
// other wise we can write string path = Server.MapPath(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();

}

else
{

Response.Write("This file does not exist.");

}

No comments: