httpHandler Content-Length太短了?

时间:2014-07-24 13:57:54

标签: c# asp.net .net vb.net

我正在尝试通过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.NET

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()

1 个答案:

答案 0 :(得分:0)

尝试在Response.End之前放置Response.Flush。流有时可能很古怪。

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