I am trying to programatically download a file through clicking a link, on my site (it's a .doc file sitting on my web server). This is my code:
string File = Server.MapPath(@"filename.doc");
string FileName = "filename.doc";
if (System.IO.File.Exists(FileName))
{
FileInfo fileInfo = new FileInfo(File);
long Length = fileInfo.Length;
Response.ContentType = "Application/msword";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", Length.ToString());
Response.WriteFile(fileInfo.FullName);
}
This is in a buttonclick event handler. Ok I could do something about the file path/file name code to make it neater, but when clicking the button, the page refreshes. On localhost, this code works fine and allows me to download the file ok. What am I doing wrong?
Thanks