I copied a code piece to send files to browser. I don't know why we use the lines written below cause removing these does not make any difference in my development environment.
Response.Clear();
Response.ClearHeaders();
Respon开发者_StackOverflowse.Buffer = false;
Can anyone provide a simple break down of the intended purpose & appropriateness of these.
Thanks
Response.Clear();
If you have written anything out already to the buffer, you'll need to clear that so extraneous content doesn't get included.
Response.ClearHeaders();
If the content type had been previously specified, for example, you probably don't want that. Any number of HTTP headers may have already been set - cache-control is another good example.
Response.Buffer = false;
No sense buffering the output if you're ready to dump the file out... just send it along and don't waste memory.
Response.ClearHeaders
assure that no headers are sent to the client. You need that because, before that function or event, the page could have sent some headers, for instance content-type or cache-control. You need Response.Clear
because the page could have rendered some html in the buffer.
***
> HttpResponse.Buffer Property
***
Gets or sets a value indicating whether to buffer output and send it after the complete response is finished processing.
true if the output to client is buffered; otherwise, false.
***
> HttpResponse.Clear Method
***
Clears all content output from the buffer stream
The following example sets the ContentType property for the response to image/jpeg, calls the Clear method to remove other content that might be attached to the response, and then sets the BufferOutput property to true so that the complete page will be processed before any content is sent to the requesting client.
// Set the page's content type to JPEG files
// and clears all content output from the buffer stream.
Response.ContentType = "image/jpeg";
Response.Clear();
// Buffer response so that page is sent
// after processing is complete.
Response.BufferOutput = true;
***
> HttpResponse.ClearHeaders Method
***
Clears all headers from the buffer stream.
The following example calls the ClearHeaders method to ensure that no headers are sent with the current response. This technique can be especially important if the ASP.NET response is generating an image, such as a JPEG file. In this example the ContentType property is set to image/jpeg.
// Clear headers to ensure none
// are sent to the requesting browser
// and set the content type.
Response.ClearHeaders();
Response.ContentType = "image/jpeg";
-------------------------------------------------------------------------
summary :
asp.net keeps the header in a collection (Response.Headers) and the content in an output stream (Response.Output).
Response.ClearHeaders - clears the current header collection
Response.Clear - resets the output stream
Response.ClearContent call Clear and is just a better name
all three will fail if Response.Buffer == false, and any output was been written.
精彩评论