ASP.NET Response.OutputStream问题

时间:2011-01-05 10:45:53

标签: .net asp.net httpresponse

我正在尝试使用Response.OutputStream将二进制数据发送到客户端,但似乎遇到了问题。我的代码非常简单,几乎与之前使用过的代码完全相同,但代码运行时客户端没有任何反应。

Response.Buffer = false;
Response.ContentType = @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("content-length", genstream.Length.ToString());
Response.AppendHeader("content-disposition", string.Format("attachment; filename={0}.xlsx", filename));

byte[] buffer = new byte[1024];
genstream.Position = 0;

int n;
while ((n  = genstream.Read(buffer, 0, 1024) ) > 0)
{
    Response.OutputStream.Write(buffer, 0, n);
}

如果有代码,那么它似乎是好的,还有什么可能导致这种行为?

1 个答案:

答案 0 :(得分:2)

使用Response BinaryWrite函数和Flush命令。 E.g:

Response.ContentType = @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(buffer);
Response.Flush();