httpHandler Content-Length too short?

本文关键字:short too Content-Length httpHandler | 更新日期: 2023-09-27 18:14:31

我试图通过httpHandler下载PDF文件,但由于某种原因,我的httpHandler返回不正确的内容长度大小?

如果我将此代码放入正常的aspx页面,它可以工作,所以我只能猜测httpHandlers有一些不同的东西?

代码如下(在c#和vb.net中),我返回一个额外的my-length头,看看我的lData数组的长度是什么

当我在chrome中执行此操作时,content-length属性是842710,但my-length是845942

为什么它们不同?我甚至尝试手动设置内容长度,但这没有任何作用…

任何帮助都非常感谢

c#

byte[] lData = Document.GetData();
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
' headers for response
context.Response.AddHeader("My-Length", lData.Length);
context.Response.AddHeader("Content-Type", "application/pdf");
context.Response.AddHeader("Content-Disposition", "attachment; filename="""MyFile.PDF"""");
context.Response.OutputStream.Write(lData, 0, lData.Length);
context.Response.End();

VB。净

Dim lData() As Byte = lDocument.GetData()
context.Response.Clear()
context.Response.ClearContent()
context.Response.ClearHeaders()
' headers for response
context.Response.AddHeader("My-Length", lData.Length)
context.Response.AddHeader("Content-Type", "application/pdf")
context.Response.AddHeader("Content-Disposition", "attachment; filename="""MyFile.PDF"""")
context.Response.OutputStream.Write(lData, 0, lData.Length)
context.Response.End()

httpHandler Content-Length too short?

尝试放置一个响应。在响应之前刷新。结束。流有时候会很奇怪。

context.Response.OutputStream.Write(lData, 0, lData.Length);
context.Response.Flush();
context.Response.End();