BinaryWrite Flush的结果缺失

时间:2016-03-24 10:24:03

标签: c# asp.net

我在处理从服务器下载文件时遇到一些问题 问题是缺少下载文件的末尾。

我发现其他人有类似问题的迹象,但没有什么可以帮助我解决问题。

调试时我已经知道fileData的长度是正确的,并且在调用BinaryWrite时所有数据都存在于字节数组中。
这留下了BinaryWriteFlushClose来电...... 我读过有关不使用Response.EndResponse.Close的内容,例如: HttpResponse.End vs HttpResponse.Close vs HttpResponse.SuppressContent这似乎是一个可能的原因,但我应该使用什么(尝试完全删除Response.Close,但这会导致数据输出过多)?

有人知道可能导致此行为的原因,以及如何解决此问题?

编辑:刚试过Response.ContentType = "binary/octet-stream";,就像魅力一样! text / plain和binary / octet-stream之间有什么区别可能导致这样的行为?
它甚至可以在没有Close调用...

的情况下工作

更多编辑:似乎在服务器端激活了响应的压缩。显然,当压缩处于活动状态时,纯文本流似乎存在问题。

我的代码是:

private void DownloadFile(byte[] fileData, string fileName, string fileExtension)
{
    Response.Clear(); 
    Response.AddHeader("content-disposition", "attachment; filename=" + fileName + fileExtension);
    Response.AddHeader("Content-Length", fileData.Length.ToString(CultureInfo.InvariantCulture));
    Response.ContentType = "text/plain";
    Response.BinaryWrite(fileData);
    Response.Flush();
    Response.Close();
}

1 个答案:

答案 0 :(得分:0)

如果服务器端(IIS)上的压缩处于活动状态,则显然会导致文本/普通流出现问题。

对于有类似问题的人,请尝试停用,这可能会有所帮助!

肯定对我有用了!