问题
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();
Response.Buffer = false;
Can anyone provide a simple break down of the intended purpose & appropriateness of these.
Thanks
回答1:
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.
回答2:
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.
回答3:
***
> 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.
来源:https://stackoverflow.com/questions/6592864/why-do-we-use-response-clearheaders