0

I have a ASP.NET application with C#. I need to send over large text string generated on the fly from the server. How do I stream my response instead of saving everything in memory (ie, in a string variable) then send in everything at the end?

So here is some more information. I generate my text on the fly like this:

List<Row> results = getRows();

and I would like to stream out like this:

foreach(Row curRow in results){
     HttpContext.Current.Response.Write(Row.data1 + " " + Row.data2);
} 
Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174
  • You can write to the Response.Outputstream. You have to make sure to do Response.Flush() at intervals to send over the data. – Nick Bray Dec 30 '13 at 20:16

2 Answers2

0

you can use WebAPI or a Handler, the HttpContext.Request has a stream. Get that stream and read it. You could read it into a memoryStream or write it to a file stream etc...

T McKeown
  • 12,971
  • 1
  • 25
  • 32
0

You can add Response.Flush() to your existing code to allow this.

foreach(Row curRow in results){
    HttpContext.Current.Response.Write(Row.data1 + " " + Row.data2);
    HttpContext.Current.Response.Flush();
}

A word of warning though, you will get an exception if you then try to change anything in the headers (cookies, or anything else) after the first Flush() has been called.

siva.k
  • 1,344
  • 14
  • 24