Home > DeveloperSection > Articles > Getting Data in chunks from ASP.net server

Getting Data in chunks from ASP.net server


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 2270  View(s)
Rate this:

Getting Data in chunks from ASP.net server

Introduction

I would like to share a way by which server may transfer the data to client in chunks rather than sending entire data.

This approach is useful when we have large data at server and client is waiting for entire page to be display. Because by default buffering is enabled in ASP.net which means that all the output from your ASP is actually sent to the browser only when the page completes its processing.

Objective

In this article we will learn following thing

·         Response.flush

Problem Statement

Client might have to wait until all the processing get completed on server, because server will send response back to client only when page completes its processing.

1)       If server is taking lot of time to process the request then Client has to wait for long time which is not good approach.

2)       Servers have to retain huge amount of data at memory.

 

Solution

Now as know, by default buffering is enabled so client must have to wait until all the processing done by server.

We can use Response.flush to send the buffered output to client.

Response.flush method is used to send buffered output immediately when the Response.Buffer is set true.

By default it is Response.Buffer= true.

Consider following line of code.

for (int i = 0; i < 100; i++)

            {

                Thread.Sleep(1000);

                Response.Write(i + "\n");

               

            }

Here in this code client have to wait for 100 sec because Thread.Sleep(1000)= 1 sec and we have loop for 0 to 99. So in above code Client has to wait which is not a good.

 

Using Response.flush

for (int i = 0; i < 100; i++)

            {

                Thread.Sleep(1000);

                Response.Write(i + "\n");

                Response.Flush()

            }

 

Here from above code, Client do not need to wait for 100 sec, client will get output on each and every second.

Data will be transferred to client in chunks. We would see updated screen after each and every second.

When I debug the Httprequest and Httpresponse, found that data has been transferred in chunk.

Following is the snap shot for above solution approach. Here we can see Transfer-encoding : Chunked.

Getting Data in chunks from ASP.net server


Don't want to miss updates? Please click the below button!

Follow MindStick