我正在从数据网格生成一个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标签。什么可能是错的?
答案 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();