0

我的 C# 代码隐藏中有一个方法可以连接到 SQL 并根据 SQL 数据创建一个 CSV 文件。当用户单击网站上的特定按钮时调用此方法。我正在尝试使用相同的方法在生成 CSV 文件后提示用户“打开”或“另存为”。

我尝试了以下方法:

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.AddHeader("Content-Disposition", "attachment; filename=" + "C:\\temp\\report.csv" + ";");           
Response.TransmitFile("C:\\temp\\report.csv");

确实会提示用户“打开或另存为”。无论哪种情况,CSV 文件中的数据还包含我的 Default.aspx 的整个源代码。我需要对我的代码执行什么操作以防止将额外数据附加到我的 CSV 中?蒂亚...

4

2 回答 2

1

您需要先清除响应 -Response.Clear();

response.Clear();
...
response.AddHeader("Content-Disposition", "attachment; filename=" + "C:\\temp\\report.csv" + ";");           
Response.TransmitFile("C:\\temp\\report.csv");
于 2012-10-13T00:03:18.677 回答
1

您可以致电response.End()以阻止您的页面进一步处理。

于 2012-10-13T00:03:25.790 回答