views:

1044

answers:

2

What is the best way to implement, from a web page a download action using asp.net 2.0?

Log files for a action are created in a directory called [Application Root]/Logs. I have the full path and want to provide a button, that when clicked will download the log file from the IIS server to the users local pc.

+10  A: 

Does this help:

http://www.west-wind.com/weblog/posts/76293.aspx

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment; filename=logfile.txt");
Response.TransmitFile( Server.MapPath("~/logfile.txt") );
Response.End();

Response.TransmitFile is the accepted way of sending large files, instead of Response.WriteFile.

Martin
A KEY part of this is the Response.End() - without it you will end up with occasionally corrupt downloads, broken digital signatures, all sorts of weirdness.
Jason Short
A: 

http://forums.asp.net/p/1481083/3457332.aspx

string filename = @"Specify the file path in the server over here....";
FileInfo fileInfo = new FileInfo(filename);

if (fileInfo.Exists)
{
   Response.Clear();
   Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + fileInfo.Name);
   Response.AddHeader("Content-Length", fileInfo.Length.ToString());
   Response.ContentType = "application/octet-stream";
   Response.Flush();
   Response.WriteFile(fileInfo.FullName);
   Response.End();
}
BiLaL
-1: As Martin said, use TransmitFile instead of WriteFile. WriteFile is essentially broken for large files
nikie