使用 asp.net 2.0 从网页实现下载操作的最佳方法是什么?

操作的日志文件在名为 [Application Root]/Logs 的目录中创建。我有完整路径并想提供一个按钮,单击该按钮会将日志文件从 IIS 服务器下载到用户本地电脑。

有帮助吗?

解决方案

这有帮助吗:

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 是发送大文件的可接受方式,而不是 Response.WriteFile。

其他提示

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();
}


更新:

初始代码

Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + fileInfo.Name);

有“内联;附件”,即内容处置的两个值。

不知道具体什么时候开始,但在 Firefox 中 仅有的 没有出现正确的文件名。将出现文件下载框,其中包含网页名称及其扩展名(页面名称.aspx)。下载后,如果您将其重命名回实际名称;文件打开成功。

按照 这一页, ,它运行于 先到先得的原则. 。将值更改为 attachment 才解决了问题。

附:我不确定这是否是最佳实践,但问题已解决。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top