What is the best way to implement, from a web page a download action using asp.net 2.0?
从网页上实现使用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.
在名为[Application Root] / Logs的目录中创建操作的日志文件。我有完整的路径,并希望提供一个按钮,单击该按钮将从IIS服务器下载日志文件到用户本地PC。
2 个解决方案
#1
34
Does this help:
这有用吗:
http://www.west-wind.com/weblog/posts/76293.aspx
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.
Response.TransmitFile是发送大文件的可接受方式,而不是Response.WriteFile。
#2
10
http://forums.asp.net/p/1481083/3457332.aspx
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", "attachment; filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Flush();
Response.TransmitFile(fileInfo.FullName);
Response.End();
}
Update:
更新:
The initial code
初始代码
Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + fileInfo.Name);
has "inline;attachment" i.e. two values for Content Disposition.
具有“内联;附件”,即内容处置的两个值。
Don't know when exactly it started, but in Firefox only the proper file name was not appearing. The file download box appears with the name of the webpage and its extension (pagename.aspx). After download, if you rename it back to the actual name; file opens successfully.
不知道它何时开始,但在Firefox中只显示正确的文件名。将显示文件下载框,其中包含网页名称及其扩展名(pagename.aspx)。下载后,如果将其重命名为实际名称;文件打开成功。
As per this page, it operates on First Come First Served basis. Changing the value to attachment
only solved the issue.
根据此页面,它按先到先得的原则运作。将值更改为附件只能解决问题。
PS: I am not sure if this is the best practice but the issue is resolved.
PS:我不确定这是否是最佳做法,但问题已得到解决。
#1
34
Does this help:
这有用吗:
http://www.west-wind.com/weblog/posts/76293.aspx
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.
Response.TransmitFile是发送大文件的可接受方式,而不是Response.WriteFile。
#2
10
http://forums.asp.net/p/1481083/3457332.aspx
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", "attachment; filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Flush();
Response.TransmitFile(fileInfo.FullName);
Response.End();
}
Update:
更新:
The initial code
初始代码
Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + fileInfo.Name);
has "inline;attachment" i.e. two values for Content Disposition.
具有“内联;附件”,即内容处置的两个值。
Don't know when exactly it started, but in Firefox only the proper file name was not appearing. The file download box appears with the name of the webpage and its extension (pagename.aspx). After download, if you rename it back to the actual name; file opens successfully.
不知道它何时开始,但在Firefox中只显示正确的文件名。将显示文件下载框,其中包含网页名称及其扩展名(pagename.aspx)。下载后,如果将其重命名为实际名称;文件打开成功。
As per this page, it operates on First Come First Served basis. Changing the value to attachment
only solved the issue.
根据此页面,它按先到先得的原则运作。将值更改为附件只能解决问题。
PS: I am not sure if this is the best practice but the issue is resolved.
PS:我不确定这是否是最佳做法,但问题已得到解决。