开发者

Pumping Bytes Directly into Response.OutputStream - how to deal with byte count?

I have a need to transfer large streams of data from a web service. Rather than increase the IIS buffer size in the metabase, I'd like to pump the bytes directly into the unbuffered Response.OutputStream.

Response.ClearHeaders();
Response.Clear();
Response.ContentType = "text/xml";
Response.Buffer = false;
int len = GetReport(protocolName, sourceName, reportName, pageIndex, pageSize, Response.OutputStream);
Response.End();

This works fine, but I can only know how many bytes ha开发者_开发知识库ve been retrieved after retrieving them, so I can't set the ContentLength header. The following line throws an exception (can't add headers after you start returning content):

int len = GetReport(protocolName, sourceName, reportName, pageIndex, pageSize, Response.OutputStream);
Response.AddHeader("Content-Length", len.ToString()); // can't do this

Question: I guess there really aren't many options. I suppose the only way to handle this would be to introduce an intermediate stream, e.g., memory stream, get the length of the stream, set the header, and then pump that stream into the Response.Output stream. Any other ideas?'


content-length is not required. If you legitimately don't know it prior to sending content to the response stream, then leave it out. The browser will still display the result fine, but will not be able to provide a progress bar or percentage completion. If the content is small, then this is not a major concern.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13

In HTTP, it SHOULD be sent whenever the message's length can be determined prior to being transferred

That's SHOULD and not MUST.


You should modify the function to give the length before writing the data.

For example, you could make it take an Action<long> callback, then call the callback with the length before writing to the stream.
Your page could then pass length => Response.AddHeader("Content-Length", length.ToString()) as the callback.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜