IIS 7 动态压缩:这会使 Content-Length 标头不可预测吗?

发布于 2024-11-07 06:18:28 字数 972 浏览 6 评论 0原文

在 Windows 2008 R2 上的 IIS 7.5 上使用 .NET 4.0。

我想输出代表各种类型文档(图像、PDF、Office 文件等)的二进制内容。假设整个内容已经在 MemoryStream 中,我想通过以下方式输出它:

Response.Clear();
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileNameSaveAs));
Response.AddHeader("Content-Length", memoryStr.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.OutputStream.Write(memoryStr.ToArray(), 0, (int) memoryStr.Length);
Response.Flush();

上面的代码不可靠。经常会出现文件损坏的情况。使用各种浏览器的客户端有时会中止下载,有时会下载无法读取的文件。损坏的可能性随着文件大小的增加而增加。使用 fiddler,我们发现响应标头报告的内容长度与原始文件大小不同。因此,为了进行快速测试,我们注释掉了 Response.AddHeader("Content-Length" ...) 行,损坏问题消失了。

Q1:这个问题是由动态压缩(IIS7默认启用)引起的吗?

Q2:如果 Q1 的答案是肯定的,那么是否有任何优雅的解决方案来通知客户端有关 Content-Length 的信息?

Q3:删除“Content-Length”标头似乎会影响客户端将文件另存为的能力。示例:“Content-Disposition”通过 fileNameSaveAs =“One Two Three.pdf”进行初始化。使用 Firefox,当接收文件时,下载对话框默认为“One”作为文件名。这是正常的后果吗?

预先感谢您的任何帮助。

Using .NET 4.0 on IIS 7.5 on Windows 2008 R2.

I would like to output a binary content which represents various types of documents (images, PDF, Office files, etc). Let's assume the entire content is already in a MemoryStream, I would like to output it by:

Response.Clear();
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileNameSaveAs));
Response.AddHeader("Content-Length", memoryStr.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.OutputStream.Write(memoryStr.ToArray(), 0, (int) memoryStr.Length);
Response.Flush();

The code above is not reliable. There are often file corruption. The clients using various browsers, sometimes have an aborted download, sometimes download a file which is unreadable. The likelihood of having a corruption increases with the file size. Using fiddler, we found out that the response header reported a content length different than the original file size. So for a quick test we commented out the line Response.AddHeader("Content-Length" ...) and the corruption issue disappeared.

Q1: Is this issue caused by the Dynamic Compression (enabled on IIS7 by default)?

Q2: If answer to Q1 is yes, then is there any elegant solution to inform the client about the Content-Length?

Q3: Removing the "Content-Length" header seems to affect the ability of the client to save file as. Example: "Content-Disposition", is initalized with fileNameSaveAs = "One Two Three.pdf". Using Firefox, when receiving the file, the download dialog defaulted to "One" as filename. Is it a normal consequence?

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月野兔 2024-11-14 06:18:28

做了更多测试,澄清了一些事情,但技术上并不令人满意。

A1。 IIS 7.5 动态压缩不是原因。无论动态压缩、静态压缩或两者都被禁用,下载损坏仍然发生。一旦代码中注释掉了 Response.AddHeader("Content-Length" ... 行。所有下载问题都消失了。A2

.不知道!我真的很想知道。A3

.这种行为可能是 Firefox 的错误这与“Content-Length”标头无关。

made more tests, clarified a few things but not technically satisfactory.

A1. IIS 7.5 Dynamic Compression is not the cause. Download corruption still occurred whether Dynamic Compression, Static Compression, or both is disabled. As soon as the line Response.AddHeader("Content-Length" ... is commented out in the code. All download issue disappeared.

A2. no idea! I really would like to know.

A3. This behavior is probably a Firefox bug. This has nothing to do with the "Content-Length" header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文