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 have 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-lengthis 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
That’s SHOULD and not MUST.