Following the The easiest way to export to MS Excel, I came across with another problem: if we make a request to download a big file, the server will wait to generate the complete file before sending it to the user agent. This is kind of bad, because the user will make the request, and will see the page "Downloading" with no progress at all.

We can change this and send some portions of the file from time to time, as soon as the became available. To accomplish this we can use the Response.Flush. The following example flushes the response every 100 objects:

C#:
  1. protected override void Render( HtmlTextWriter writer )
  2. {
  3.     int count = 0;
  4.     foreach( Entity e in Collection ) {
  5.         Write(e);
  6.         if( ++count % 100 ) {
  7.             Response.Flush();
  8.         }
  9.     }
  10. }

Note that funny things will happen if you redirect after starting the download! ;)

Related Posts

One Response to “ASP.NET - Continuous Dynamic Downloading”

  1. нeжнaянoчь Says:

    Все-таки на самом деле нравится мне Ваш блог. Всегда занятно читать, включая эту тему. :)