Asp. NET output stream must have at least 256 bytes of data after the Response.Flush method takes effect

Source: Internet
Author: User

Most of the time we write an ASP that will take a minute or even a few minutes because it does a lot of work. To enable software users to wait patiently for the execution of a program, we often want a progress bar to represent the state of the program execution. Or at the very least, display descriptive text such as "Data Loading", "Saving data", and so on. At this point we will use the Response.Flush (). He will send the data compiled in the buffer to the client first.

But there are times when we find that even if we use Response.Flush (), we do not send the data already in the output stream to the client. The client presented to us is still white screen. After repeated testing, I came to a conclusion (representing a personal point of view, freely quoted, but with the consequences). That is, flush must have at least 256 bytes of content. That is, at least 256 bytes of data in the HTTP output stream, which can be sent to the client when Response.Flush () is executed.

We can use a piece of code to test the simulation to download a 1MB size Excel file, but we do not want all the file data to be loaded into the server output stream before the data sent to the client browser to start the download, Instead, you want to call Response.Flush () to send data to the client browser to start the download immediately after some data is output to the server output stream.

Response.ContentType ="Application/vnd.ms-excel"; Response.appendheader ("Content-length", (1024x768*1024x768). ToString ());//tells the client that the total output file size is 1MBResponse.appendheader ("content-disposition","Attachment;filename=output.xls");//analog output 256 bytes to output stream, loop 256 times, output one byte at a time for(vari =0; I < the; i++) {Response.BinaryWrite (New byte[] {0x01});} Response.Flush ();//when the above loop outputs 256 bytes to the output stream, Response.Flush will send the data to the client without a single byte.Response.End ();

You can try it. The loop in the above code must be 256 times (that is, output 256 bytes to the output stream) Response.Flush method will send data to the client, less than once ...

Asp. NET output stream must have at least 256 bytes of data after the Response.Flush method takes effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.