用asp.net生成的csv也包含html代码

时间:2013-11-14 13:36:33

标签: asp.net csv http-headers httpcontext

我正在从数据网格生成一个csv并使用以下代码下载它:

var context = HttpContext.Current;
                context.Response.ClearHeaders();            
                context.Response.ContentType = "application/vnd.ms-excel";
                context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
                context.Response.Write(csvString);
                Response.Flush();

csv下载,但在它下面,有页面的html标签。什么可能是错的?

1 个答案:

答案 0 :(得分:3)

您应该致电Response.End(),以便在发送csv后,您的页面其余部分无法呈现。在没有Response.End的情况下,在发送此内容后,页面将正常呈现。

var context = HttpContext.Current;
context.Response.ClearHeaders();            
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
context.Response.Write(csvString);
Response.End();