在 ASP.NET 中发送压缩响应

发布于 2024-07-25 03:57:28 字数 407 浏览 7 评论 0原文

我正在 IIS6 上为 ASP.NET 应用程序运行一个网站,并启用了压缩,这对于 .aspx 网页效果很好。

不起作用的是下载的二进制文件,这些文件作为回发响应的一部分传输:例如链接按钮是“下载到 Excel” - 用户单击,代码生成一个二进制文件,执行 Response.Clear(),然后写入文件到 Response.OutputStream。

浏览器可以看到响应,但它始终为零字节。 因此,我假设网络浏览器期望压缩响应,并且由于原始二进制文件不是有效的压缩流,因此它会失败。 我很困惑如果我已经清除了响应,为什么会出现这种情况 - 当然响应标头(指定压缩)也被清除了?

因此出现了两个问题:

1)如何压缩二进制文件以发送压缩响应? 2)如何在运行时检查IIS压缩是否启用?

干杯

I am running a website on IIS6 for ASP.NET application and enabled compression, which worked fine for the .aspx web pages.

What isn't working is downloaded binary files that are transmitted as part of a postback response: e.g. link button is 'download to excel' - user clicks and the code generates a binary file, does a Response.Clear() and then writes the file to the Response.OutputStream.

A response is seen by browsers but it is always zero bytes. I assume therefore that web browsers are expecting a compressed response, and as the raw binary isn't a valid compressed stream it fails. I'm puzzled as to why this should be if I've cleared the response - surely the response headers (specifying compression) are also cleared?

So two questions arise:

1) how do I compress the binary file to send a compressed response?
2) how can I check at runtime to see if IIS compression is enabled?

Cheers

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

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

发布评论

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

评论(1

公布 2024-08-01 03:57:28

我会禁用压缩并检查这是否仍然有效,只是为了隔离这确实是由于 IIS 压缩造成的。 我告诉你,因为我正在运行一个启用 IIS/压缩的网站,它可以毫无问题地提供 PDF 文件(二进制)。

无论如何,这是对我有用的代码部分。

 Response.Clear();
 Response.ClearHeaders();

 Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
 Response.AddHeader("Content-transfer-encoding", "8bit");
 Response.AddHeader("Cache-Control", "private");
 Response.AddHeader("Pragma", "cache");
 Response.AddHeader("Expires", "0");  

 Response.ContentType = "application/pdf";
 Response.WriteFile(filePath);

 Response.Flush();
 Response.Close();
 HttpContext.Current.ApplicationInstance.CompleteRequest();

I would disable compression and check whether this still works, just to isolate the fact that this is indeed due to IIS compression. I'm telling you that 'cos I'm running a IIS/Compression enabled site which provides PDF files (binary) without a problem.

Anyway here's the part of code which works for me.

 Response.Clear();
 Response.ClearHeaders();

 Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
 Response.AddHeader("Content-transfer-encoding", "8bit");
 Response.AddHeader("Cache-Control", "private");
 Response.AddHeader("Pragma", "cache");
 Response.AddHeader("Expires", "0");  

 Response.ContentType = "application/pdf";
 Response.WriteFile(filePath);

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